Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions Week_03/id_39/LeetCode_102_39.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number[][]}
* https://leetcode.com/problems/binary-tree-level-order-traversal/
*/
var levelOrder = function(root) {
if (!root) {
return [];
}
let result = [];
let level = [root];
let queue = [];
let depth = -1;

while (level.length) {
result[++depth] = [];

while (level.length) {
let node = level.shift();

if (node.val != null) {
result[depth].push(node.val);
}


if (node.left) {
queue.push(node.left);
}
if (node.right) {
queue.push(node.right);
}
}

level = queue;
queue = [];
}

return result;
};
50 changes: 50 additions & 0 deletions Week_03/id_39/LeetCode_103_39.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number[][]}
* https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/
*/
var zigzagLevelOrder = function(root) {
if (!root) {
return [];
}
let queue = [root];
let isOdd = true;
let levelNum = 1;
let result = [];

while (queue.length) {
result.push([]);
let level = result.length - 1;
let count = 0;
while (levelNum) {
levelNum--;
let node = queue.shift();
if (node.val != null) {
if (isOdd) {
result[level].push(node.val);
} else {
result[level].unshift(node.val);
}
}
if (node.left) {
queue.push(node.left);
count++;
}
if (node.right) {
queue.push(node.right);
count++;
}
}
levelNum = count;
isOdd = !isOdd;
}

return result;
};
41 changes: 41 additions & 0 deletions Week_03/id_39/LeetCode_107_39.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number[][]}
* https://leetcode.com/problems/binary-tree-level-order-traversal-ii/
*/
var levelOrderBottom = function(root) {
if (!root) {
return [];
}
let queue = [root];
let levelNum = 1;
let result = [];

while (queue.length) {
result.unshift([]);
let count = 0;
for (let i = 0; i < levelNum; i++) {
let node = queue.shift();
if (node.val != null) {
result[0].push(node.val);
}
if (node.left) {
queue.push(node.left);
count++;
}
if (node.right) {
queue.push(node.right);
count++;
}
}
levelNum = count;
}
return result;
};
101 changes: 101 additions & 0 deletions Week_03/id_39/LeetCode_200_39.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* @param {character[][]} grid
* @return {number}
* https://leetcode.com/problems/number-of-islands/
*/

/**
* 以下为 dfs递归染色解法
*/
var dfs = function (grid, i, j) {
let direction = [[1, 0], [-1, 0], [0, 1], [0, -1]];
grid[i][j] = '0';
for (let h = 0; h < direction.length; h++) {
let x = direction[h][0];
let y = direction[h][1];
if (grid[i+x] && grid[i+x][j+y] == '1') {
grid[i+x][j+y] = '0';
dfs(grid, i+x, j+y);
}
}
}

var numIslands = function(grid) {
let count = 0;
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[i].length; j++) {
if (grid[i][j] == '1') {
count++;
dfs(grid, i, j);
}
}
}
return count;
};


/**
* 以下为用并查集的解法
*/
class UnionFind{
constructor(grid) {
let m = grid.length, n = grid[0].length;
this.count = 0;
this.parent = new Array(m*n).fill(-1);
this.rank = new Array(m*n).fill(0);
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (grid[i][j] == '1') {
this.parent[i * n + j] = i * n + j;
this.count++;
}
}
}
}
find(i) {
if (this.parent[i] != i) {
this.parent[i] = this.find(this.parent[i]);
}
return this.parent[i];
}
union(x, y) {
let rootx = this.find(x);
let rooty = this.find(y);
if (rootx != rooty) {
if (this.rank[rootx] > this.rank[rooty]) {
this.parent[rooty] = rootx;
} else if (this.rank[rootx] < this.rank[rooty]) {
this.parent[rootx] = rooty;
} else {
this.parent[rooty] = rootx;
this.rank[rootx] += 1;
}
this.count--;
}
}
}

var numIslands = function(grid) {
if (!grid || !grid[0]) {
return 0;
}
let uf = new UnionFind(grid);
let direction = [[1, 0], [-1, 0], [0, 1], [0, -1]];
let m = grid.length, n = grid[0].length;

for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (grid[i][j] == '0') {
continue;
}
for (let d = 0; d < direction.length; d++) {
let nr = i + direction[d][0], nc = j + direction[d][1];
if (nr >= 0 && nc >= 0 && nr < m && nc < n && grid[nr][nc] == '1') {
uf.union(i * n + j, nr * n + nc);
}
}
}
}

return uf.count;
};
41 changes: 41 additions & 0 deletions Week_03/id_39/LeetCode_329_39.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* @param {number[][]} matrix
* @return {number}
* https://leetcode.com/problems/longest-increasing-path-in-a-matrix/
* dfs + 记忆化
*/

var direction = [[0, 1], [1, 0], [-1, 0], [0, -1]];

var dfs = function (matrix, i, j, memo) {
let m = matrix.length, n = matrix[0].length;
let index = i * n + j;
if (memo[index] != -1) {
return memo[index];
}
let max = 1;

for (let d = 0; d < direction.length; d++) {
let x = i + direction[d][0], y = j + direction[d][1];
if (x >= 0 && y >= 0 && x < m && y < n && matrix[x][y] > matrix[i][j]) {
max = Math.max(max, dfs(matrix, x, y, memo) + 1);
}
}
memo[index] = max;
return max;
}

var longestIncreasingPath = function(matrix) {
if (!matrix || !matrix.length) {
return 0;
}
let m = matrix.length, n = matrix[0].length;
let max = 1;
let memo = new Array(m*n).fill(-1);
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
max = Math.max(max, dfs(matrix, i, j, memo));
}
}
return max;
};
43 changes: 43 additions & 0 deletions Week_03/id_39/LeetCode_429_39.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* // Definition for a Node.
* function Node(val,children) {
* this.val = val;
* this.children = children;
* };
*/
/**
* @param {Node} root
* @return {number[][]}
* https://leetcode.com/problems/n-ary-tree-level-order-traversal/
*/
var levelOrder = function(root) {
if (!root) {
return [];
}
let result = [];

let queue = [];
let level = [root];
let depth = -1;

while (level.length) {
depth++;
result[depth] = [];

while (level.length) {
let node = level.shift();
result[depth].push(node.val);

if (!node.children) {
continue;
}
for (let i = 0; i < node.children.length; i++) {
queue.push(node.children[i]);
}
}

level = queue;
queue = [];
}
return result;
};
37 changes: 37 additions & 0 deletions Week_03/id_39/LeetCode_529_39.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @param {character[][]} board
* @param {number[]} click
* @return {character[][]}
* https://leetcode.com/problems/minesweeper/
*/
var updateBoard = function(board, click) {
let m = board.length, n = board[0].length;
let direction = [[0, 1], [1, 0], [0, -1], [-1, 0], [1, 1], [-1, -1], [1, -1], [-1, 1]];

let cX = click[0];
let cY = click[1];
if (board[cX][cY] == 'M') {
board[cX][cY] = 'X';
}
if (board[cX][cY] == 'E') {
let count = 0;
for (let i = 0; i < direction.length; i++) {
let x = cX + direction[i][0], y = cY + direction[i][1];
if (x >= 0 && y >= 0 && x < m && y < n && board[x][y] == 'M') {
count++;
}
}
if (count == 0) {
board[cX][cY] = 'B';
for (let i = 0; i < direction.length; i++) {
let x = cX + direction[i][0], y = cY + direction[i][1];
if (x >= 0 && y >= 0 && x < m && y < n) {
updateBoard(board, [x, y]);
}
}
} else {
board[cX][cY] = count.toString();
}
}
return board;
};
13 changes: 13 additions & 0 deletions Week_03/id_39/NOTE.md
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
# 学习笔记

## 并查集
- 并查集的逻辑理解起来不难
- 但是写起来不是特别简单,虽然都是基础数据结构,但是还是不那么好写,尤其是带rank的,路径压缩还比较好理解,得多写几次
- 岛屿的题目用dfs来做非常好理解,突然觉得递归这个之前觉得很难的东西,在算法题里反倒是最简单的了
- 自己还是有很多成长的,加油~

## 树
- 写了很多个按层遍历BFS的题,现在已经非常熟悉使用queue来做按层遍历的题
- 之前觉得树对我是一个不小的难点,但是做的多了,都是套路化的应用dfs(stack)和bfs(queue)

## 堆
- 堆的应用其实不难,像java这种语言有直接提供优先队列使用倒是不复杂,但是像js这个,得自己实现一个大顶堆、小顶堆,是个不小的挑战。