Repository navigation
第一周作业#39 #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
第一周作业#39 #5
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8b292c7
update
1f8114f
update
d088a0e
update
10979fc
rename && add two question
702f9af
update
2b7023c
add leetcode 50 84 1021 && rename all file
98fd398
rename
cc7cd6f
add leetcode 101
0256766
add leetcode 111
7f6069a
add leetcode 257
bf7b631
add 脑图 && update 39
31f6525
add leetcode 236
28554fc
add leetcode 104
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /** | ||
| * Definition for a binary tree node. | ||
| * function TreeNode(val) { | ||
| * this.val = val; | ||
| * this.left = this.right = null; | ||
| * } | ||
| */ | ||
| /** | ||
| * @param {TreeNode} root | ||
| * @return {boolean} | ||
| * https://leetcode.com/problems/symmetric-tree/ | ||
| */ | ||
|
|
||
| // 递归法 | ||
| var treeSymmetric = function(t1, t2) { | ||
| if (t1 == null || t2 == null) { | ||
| return t1 == t2; | ||
| } | ||
| if (t1.val !== t2.val) { | ||
| return false; | ||
| } | ||
| return treeSymmetric(t1.left, t2.right) && treeSymmetric(t1.right, t2.left); | ||
| }; | ||
| var isSymmetric = function(root) { | ||
| if (!root) { | ||
| return true; | ||
| } | ||
| return treeSymmetric(root.left, root.right); | ||
| }; | ||
|
|
||
|
|
||
| // 迭代法 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /** | ||
| * @param {string} S | ||
| * @return {string} | ||
| * https://leetcode.com/problems/remove-outermost-parentheses/ | ||
| * 标志位,刨除标志位为0的时候,其余时候都移到结果中 | ||
| */ | ||
| var removeOuterParentheses = function(S) { | ||
| let sign = 0; | ||
| let result = ''; | ||
| let i = 0; | ||
| while (i < S.length) { | ||
| let cur = S.charAt(i); | ||
| if (cur === '(' && sign++ > 0) { | ||
| result += cur; | ||
| } else if (cur === ')' && sign-- > 1) { | ||
| result += cur; | ||
| } | ||
| i++; | ||
| } | ||
| return result; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /** | ||
| * @param {string} S | ||
| * @return {string} | ||
| * https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/ | ||
| */ | ||
| var removeDuplicates = function(S) { | ||
| let arr = S.split(''); | ||
| let stack = []; | ||
| for (let j = 0; j < arr.length; j++) { | ||
| if (!stack.length) { | ||
| stack.push(arr[j]); | ||
| continue; | ||
| } | ||
| let prev = stack.pop(); | ||
| if (arr[j] !== prev) { | ||
| stack.push(prev); | ||
| stack.push(arr[j]); | ||
| } | ||
| } | ||
| return stack.join(''); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /** | ||
| * 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/maximum-depth-of-binary-tree/ | ||
| */ | ||
| // 广度优先搜索 | ||
| var maxDepth = function(root) { | ||
| if (!root) { | ||
| return 0; | ||
| } | ||
|
|
||
| let queue = []; | ||
| let level = [root]; | ||
| let depth = 0; | ||
|
|
||
| while (level.length) { | ||
| depth++; | ||
|
|
||
| while (level.length) { | ||
| let node = level.shift(); | ||
| if (node.left) { | ||
| queue.push(node.left); | ||
| } | ||
| if (node.right) { | ||
| queue.push(node.right); | ||
| } | ||
| } | ||
| level = queue; | ||
| queue = []; | ||
| } | ||
|
|
||
| return depth; | ||
| }; | ||
|
|
||
| // DFS 深度优先搜索 递归形式 | ||
| var maxDepth = function(root) { | ||
| if (!root) { | ||
| return 0; | ||
| } | ||
| return Math.max(maxDepth(root.left) + 1, maxDepth(root.right) + 1); | ||
| }; | ||
|
|
||
| // DFS 深度优先搜索 非递归形式 | ||
| var maxDepth = function(root) { | ||
| if (!root) { | ||
| return 0; | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /** | ||
| * 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/minimum-depth-of-binary-tree/ | ||
| * 注意:最小深度是根节点到叶子节点,so 如果只有根节点和它都某一子节点,则深度为2 | ||
| */ | ||
|
|
||
| var minDepth = function(root) { | ||
| if (!root) { | ||
| return 0; | ||
| } | ||
| let left = minDepth(root.left); | ||
| let right = minDepth(root.right); | ||
| if (left == 0 || right == 0) { | ||
| return left + right + 1; | ||
| } | ||
| return Math.min(left, right) + 1; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /** | ||
| * 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/maximum-depth-of-binary-tree/ | ||
| */ | ||
| // 广度优先搜索 | ||
| var maxDepth = function(root) { | ||
| if (!root) { | ||
| return 0; | ||
| } | ||
|
|
||
| let queue = []; | ||
| let level = [root]; | ||
| let depth = 0; | ||
|
|
||
| while (level.length) { | ||
| depth++; | ||
|
|
||
| while (level.length) { | ||
| let node = level.shift(); | ||
| if (node.left) { | ||
| queue.push(node.left); | ||
| } | ||
| if (node.right) { | ||
| queue.push(node.right); | ||
| } | ||
| } | ||
| level = queue; | ||
| queue = []; | ||
| } | ||
|
|
||
| return depth; | ||
| }; | ||
|
|
||
| // DFS 深度优先搜索 递归形式 | ||
| var maxDepth = function(root) { | ||
| if (!root) { | ||
| return 0; | ||
| } | ||
| return Math.max(maxDepth(root.left) + 1, maxDepth(root.right) + 1); | ||
| }; | ||
|
|
||
| // DFS 深度优先搜索 非递归形式 | ||
| var maxDepth = function(root) { | ||
| if (!root) { | ||
| return 0; | ||
| } | ||
| // | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /** | ||
| * @param {number[]} nums | ||
| * @return {number[][]} | ||
| * https://leetcode.com/problems/3sum/ | ||
| * 备注:这一版太乱了, 内存性能都非常差!!! | ||
| */ | ||
|
|
||
| var threeSum = function(arrs) { | ||
| if (arrs.length < 3) { | ||
| return []; | ||
| } | ||
| let nums = []; | ||
| let map = new Map(); | ||
| for (let i = 0; i < arrs.length; i++) { | ||
| let cur = arrs[i]; | ||
| if (map.has(cur)) { | ||
| let count = map.get(cur); | ||
| if (count < 3) { | ||
| map.set(cur, count + 1); | ||
| nums.push(cur); | ||
| } | ||
| } else { | ||
| map.set(cur, 1); | ||
| nums.push(cur); | ||
| } | ||
| } | ||
| // nums = temps; | ||
| let result = new Set(); | ||
| for (let i = 0; i < nums.length; i++) { | ||
| for (let j = 0; j < nums.length; j++) { | ||
| let a = nums[i]; | ||
| let b = nums[j]; | ||
| let c = (-1) * (a + b); | ||
| if (map.has(c)) { | ||
| if ((a == b && b == c && c == a) && map.get(a) < 3) { | ||
| continue; | ||
| } | ||
| if ((a == b || a == c) && map.get(a) < 2) { | ||
| continue; | ||
| } | ||
| if (b == c && map.get(b) < 2) { | ||
| continue; | ||
| } | ||
| let arr = [a, b, c].sort().join(','); | ||
| result.add(arr); | ||
| } | ||
| } | ||
| } | ||
| return Array.from(result, (val) => { | ||
| return val.split(',') | ||
| }); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /** | ||
| * @param {number[]} nums | ||
| * @param {number} k | ||
| * @return {void} Do not return anything, modify nums in-place instead. | ||
| * https://leetcode.com/problems/rotate-array/ | ||
| * 思路:比较难想,3次reverse | ||
| * tips: 3种不同思路 | ||
| */ | ||
| var reverse = function(nums, i, j) { | ||
| while(i < j) { | ||
| temp = nums[i]; | ||
| nums[i] = nums[j]; | ||
| nums[j] = temp | ||
| i++; | ||
| j--; | ||
| } | ||
| } | ||
|
|
||
| var rotate = function(nums, k) { | ||
| let len = nums.length; | ||
| k %= len; | ||
|
|
||
| reverse(nums, 0, len - k - 1); | ||
| reverse(nums, len - k, len - 1); | ||
| reverse(nums, 0, len - 1); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /** | ||
| * Definition for singly-linked list. | ||
| * function ListNode(val) { | ||
| * this.val = val; | ||
| * this.next = null; | ||
| * } | ||
| */ | ||
| /** | ||
| * @param {ListNode} l1 | ||
| * @param {ListNode} l2 | ||
| * @return {ListNode} | ||
| * https://leetcode.com/problems/merge-two-sorted-lists/ | ||
| * 思路:新建一个链表,头指针是一个哨兵位,遍历两个链表,比较大小,小的在前,并指针移到下一位 | ||
| */ | ||
|
|
||
| function ListNode(val) { | ||
| this.val = val; | ||
| this.next = null; | ||
| } | ||
|
|
||
| var mergeTwoLists = function(l1, l2) { | ||
| if (!l1 && !l2) { | ||
| return null; | ||
| } | ||
| let head = new ListNode(null); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 看了您的代码,更深刻地理解了哨兵的说法 |
||
| let cur1 = l1; | ||
| let cur2 = l2; | ||
| let cur = head; | ||
| while(cur1 || cur2) { | ||
| if ((cur1 && !cur2) || (cur1 && cur2 && (cur2.val > cur1.val))) { | ||
| cur.next = new ListNode(cur1.val); | ||
| cur = cur.next; | ||
| cur1 = cur1.next; | ||
| continue; | ||
| } else { | ||
| cur.next = new ListNode(cur2.val); | ||
| cur = cur.next; | ||
| cur2 = cur2.next; | ||
| continue; | ||
| } | ||
| } | ||
| return head.next; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /** | ||
| * Definition for a binary tree node. | ||
| * function TreeNode(val) { | ||
| * this.val = val; | ||
| * this.left = this.right = null; | ||
| * } | ||
| */ | ||
| /** | ||
| * @param {TreeNode} root | ||
| * @param {TreeNode} p | ||
| * @param {TreeNode} q | ||
| * @return {TreeNode} | ||
| * https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ | ||
| */ | ||
|
|
||
| // 分别在左右子树查找p或者q,找到了就返回 | ||
| var lowestCommonAncestor = function(root, p, q) { | ||
| if (!root || root == p || root == q) { | ||
| return root; | ||
| } | ||
| let left = lowestCommonAncestor(root.left, p, q); | ||
| let right = lowestCommonAncestor(root.right, p, q); | ||
|
|
||
| return left == null ? right : right == null ? left : root; | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
t1, t2,最好能写成left, right, 容易懂些。