Skip to content

Commit 18a4c5b

Browse files
Merge pull request #83 from zyziyun/week1
第一周作业#39
2 parents 9db5a82 + 28554fc commit 18a4c5b

20 files changed

+615
-0
lines changed

Week_01/id_39/LeetCode_101_39.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @return {boolean}
11+
* https://leetcode.com/problems/symmetric-tree/
12+
*/
13+
14+
// 递归法
15+
var treeSymmetric = function(t1, t2) {
16+
if (t1 == null || t2 == null) {
17+
return t1 == t2;
18+
}
19+
if (t1.val !== t2.val) {
20+
return false;
21+
}
22+
return treeSymmetric(t1.left, t2.right) && treeSymmetric(t1.right, t2.left);
23+
};
24+
var isSymmetric = function(root) {
25+
if (!root) {
26+
return true;
27+
}
28+
return treeSymmetric(root.left, root.right);
29+
};
30+
31+
32+
// 迭代法

Week_01/id_39/LeetCode_1021_39.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {string} S
3+
* @return {string}
4+
* https://leetcode.com/problems/remove-outermost-parentheses/
5+
* 标志位,刨除标志位为0的时候,其余时候都移到结果中
6+
*/
7+
var removeOuterParentheses = function(S) {
8+
let sign = 0;
9+
let result = '';
10+
let i = 0;
11+
while (i < S.length) {
12+
let cur = S.charAt(i);
13+
if (cur === '(' && sign++ > 0) {
14+
result += cur;
15+
} else if (cur === ')' && sign-- > 1) {
16+
result += cur;
17+
}
18+
i++;
19+
}
20+
return result;
21+
};

Week_01/id_39/LeetCode_1047_39.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {string} S
3+
* @return {string}
4+
* https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/
5+
*/
6+
var removeDuplicates = function(S) {
7+
let arr = S.split('');
8+
let stack = [];
9+
for (let j = 0; j < arr.length; j++) {
10+
if (!stack.length) {
11+
stack.push(arr[j]);
12+
continue;
13+
}
14+
let prev = stack.pop();
15+
if (arr[j] !== prev) {
16+
stack.push(prev);
17+
stack.push(arr[j]);
18+
}
19+
}
20+
return stack.join('');
21+
};

Week_01/id_39/LeetCode_104_39.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @return {number}
11+
* https://leetcode.com/problems/maximum-depth-of-binary-tree/
12+
*/
13+
// 广度优先搜索
14+
var maxDepth = function(root) {
15+
if (!root) {
16+
return 0;
17+
}
18+
19+
let queue = [];
20+
let level = [root];
21+
let depth = 0;
22+
23+
while (level.length) {
24+
depth++;
25+
26+
while (level.length) {
27+
let node = level.shift();
28+
if (node.left) {
29+
queue.push(node.left);
30+
}
31+
if (node.right) {
32+
queue.push(node.right);
33+
}
34+
}
35+
level = queue;
36+
queue = [];
37+
}
38+
39+
return depth;
40+
};
41+
42+
// DFS 深度优先搜索 递归形式
43+
var maxDepth = function(root) {
44+
if (!root) {
45+
return 0;
46+
}
47+
return Math.max(maxDepth(root.left) + 1, maxDepth(root.right) + 1);
48+
};
49+
50+
// DFS 深度优先搜索 非递归形式
51+
var maxDepth = function(root) {
52+
if (!root) {
53+
return 0;
54+
}
55+
};

Week_01/id_39/LeetCode_111_39.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @return {number}
11+
* https://leetcode.com/problems/minimum-depth-of-binary-tree/
12+
* 注意:最小深度是根节点到叶子节点,so 如果只有根节点和它都某一子节点,则深度为2
13+
*/
14+
15+
var minDepth = function(root) {
16+
if (!root) {
17+
return 0;
18+
}
19+
let left = minDepth(root.left);
20+
let right = minDepth(root.right);
21+
if (left == 0 || right == 0) {
22+
return left + right + 1;
23+
}
24+
return Math.min(left, right) + 1;
25+
};

Week_01/id_39/LeetCode_141_39.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @return {number}
11+
* https://leetcode.com/problems/maximum-depth-of-binary-tree/
12+
*/
13+
// 广度优先搜索
14+
var maxDepth = function(root) {
15+
if (!root) {
16+
return 0;
17+
}
18+
19+
let queue = [];
20+
let level = [root];
21+
let depth = 0;
22+
23+
while (level.length) {
24+
depth++;
25+
26+
while (level.length) {
27+
let node = level.shift();
28+
if (node.left) {
29+
queue.push(node.left);
30+
}
31+
if (node.right) {
32+
queue.push(node.right);
33+
}
34+
}
35+
level = queue;
36+
queue = [];
37+
}
38+
39+
return depth;
40+
};
41+
42+
// DFS 深度优先搜索 递归形式
43+
var maxDepth = function(root) {
44+
if (!root) {
45+
return 0;
46+
}
47+
return Math.max(maxDepth(root.left) + 1, maxDepth(root.right) + 1);
48+
};
49+
50+
// DFS 深度优先搜索 非递归形式
51+
var maxDepth = function(root) {
52+
if (!root) {
53+
return 0;
54+
}
55+
//
56+
};

Week_01/id_39/LeetCode_15_39.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[][]}
4+
* https://leetcode.com/problems/3sum/
5+
* 备注:这一版太乱了, 内存性能都非常差!!!
6+
*/
7+
8+
var threeSum = function(arrs) {
9+
if (arrs.length < 3) {
10+
return [];
11+
}
12+
let nums = [];
13+
let map = new Map();
14+
for (let i = 0; i < arrs.length; i++) {
15+
let cur = arrs[i];
16+
if (map.has(cur)) {
17+
let count = map.get(cur);
18+
if (count < 3) {
19+
map.set(cur, count + 1);
20+
nums.push(cur);
21+
}
22+
} else {
23+
map.set(cur, 1);
24+
nums.push(cur);
25+
}
26+
}
27+
// nums = temps;
28+
let result = new Set();
29+
for (let i = 0; i < nums.length; i++) {
30+
for (let j = 0; j < nums.length; j++) {
31+
let a = nums[i];
32+
let b = nums[j];
33+
let c = (-1) * (a + b);
34+
if (map.has(c)) {
35+
if ((a == b && b == c && c == a) && map.get(a) < 3) {
36+
continue;
37+
}
38+
if ((a == b || a == c) && map.get(a) < 2) {
39+
continue;
40+
}
41+
if (b == c && map.get(b) < 2) {
42+
continue;
43+
}
44+
let arr = [a, b, c].sort().join(',');
45+
result.add(arr);
46+
}
47+
}
48+
}
49+
return Array.from(result, (val) => {
50+
return val.split(',')
51+
});
52+
};

Week_01/id_39/LeetCode_189_39.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {void} Do not return anything, modify nums in-place instead.
5+
* https://leetcode.com/problems/rotate-array/
6+
* 思路:比较难想,3次reverse
7+
* tips: 3种不同思路
8+
*/
9+
var reverse = function(nums, i, j) {
10+
while(i < j) {
11+
temp = nums[i];
12+
nums[i] = nums[j];
13+
nums[j] = temp
14+
i++;
15+
j--;
16+
}
17+
}
18+
19+
var rotate = function(nums, k) {
20+
let len = nums.length;
21+
k %= len;
22+
23+
reverse(nums, 0, len - k - 1);
24+
reverse(nums, len - k, len - 1);
25+
reverse(nums, 0, len - 1);
26+
};

Week_01/id_39/LeetCode_21_39.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} l1
10+
* @param {ListNode} l2
11+
* @return {ListNode}
12+
* https://leetcode.com/problems/merge-two-sorted-lists/
13+
* 思路:新建一个链表,头指针是一个哨兵位,遍历两个链表,比较大小,小的在前,并指针移到下一位
14+
*/
15+
16+
function ListNode(val) {
17+
this.val = val;
18+
this.next = null;
19+
}
20+
21+
var mergeTwoLists = function(l1, l2) {
22+
if (!l1 && !l2) {
23+
return null;
24+
}
25+
let head = new ListNode(null);
26+
let cur1 = l1;
27+
let cur2 = l2;
28+
let cur = head;
29+
while(cur1 || cur2) {
30+
if ((cur1 && !cur2) || (cur1 && cur2 && (cur2.val > cur1.val))) {
31+
cur.next = new ListNode(cur1.val);
32+
cur = cur.next;
33+
cur1 = cur1.next;
34+
continue;
35+
} else {
36+
cur.next = new ListNode(cur2.val);
37+
cur = cur.next;
38+
cur2 = cur2.next;
39+
continue;
40+
}
41+
}
42+
return head.next;
43+
};

Week_01/id_39/LeetCode_236_39.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @param {TreeNode} p
11+
* @param {TreeNode} q
12+
* @return {TreeNode}
13+
* https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/
14+
*/
15+
16+
// 分别在左右子树查找p或者q,找到了就返回
17+
var lowestCommonAncestor = function(root, p, q) {
18+
if (!root || root == p || root == q) {
19+
return root;
20+
}
21+
let left = lowestCommonAncestor(root.left, p, q);
22+
let right = lowestCommonAncestor(root.right, p, q);
23+
24+
return left == null ? right : right == null ? left : root;
25+
};

0 commit comments

Comments
 (0)