Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fa2e7e4

Browse files
author
lucifer
committed
2 parents d971646 + f04b4f9 commit fa2e7e4

14 files changed

+596
-8
lines changed

README.en.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@ The data structures mainly includes:
112112
113113
#### Easy
114114

115+
- [0001.TwoSum](./problems/1.TwoSum.md)🆕
115116
- [0020.Valid Parentheses](./problems/20.validParentheses.md)
117+
- [0021.MergeTwoSortedLists](./problems/21.MergeTwoSortedLists.md) 🆕
116118
- [0026.remove-duplicates-from-sorted-array](./problems/26.remove-duplicates-from-sorted-array.md)
117119
- [0053.maximum-sum-subarray](./problems/53.maximum-sum-subarray-en.md) 🆕
118120
- [0088.merge-sorted-array](./problems/88.merge-sorted-array.md)
@@ -136,9 +138,10 @@ The data structures mainly includes:
136138
- [0263.ugly-number](./problems/263.ugly-number.md)
137139
- [0283.move-zeroes](./problems/283.move-zeroes.md)
138140
- [0342.power-of-four](./problems/342.power-of-four.md)
141+
- [0371.sum-of-two-integers](./problems/371.sum-of-two-integers.md)
139142
- [0349.intersection-of-two-arrays](./problems/349.intersection-of-two-arrays.md)
140143
- [0437.path-sum-iii](./problems/437.path-sum-iii.md) 🆕
141-
- [0371.sum-of-two-integers](./problems/371.sum-of-two-integers.md)
144+
- [0455.AssignCookies](./problems/455.AssignCookies.md) 🆕
142145
- [0501.find-mode-in-binary-search-tree](./problems/501.Find-Mode-in-Binary-Search-Tree.md) 🆕
143146
- [0575.distribute-candies](./problems/575.distribute-candies.md)
144147

@@ -149,7 +152,9 @@ The data structures mainly includes:
149152
- [0005.longest-palindromic-substring](./problems/5.longest-palindromic-substring.md)
150153
- [0011.container-with-most-water](./problems/11.container-with-most-water.md)
151154
- [0015.3-sum](./problems/15.3-sum.md)
155+
- [0017.Letter-Combinations-of-a-Phone-Number](./problems/17.Letter-Combinations-of-a-Phone-Number.md) 🆕
152156
- [0019. Remove Nth Node From End of List](./problems/19.removeNthNodeFromEndofList.md)
157+
- [0022.GenerateParentheses](./problems/22.GenerateParentheses.md) 🆕
153158
- [0024. Swap Nodes In Pairs](./problems/24.swapNodesInPairs.md)
154159
- [0029.divide-two-integers](./problems/29.divide-two-integers.md)
155160
- [0031.next-permutation](./problems/31.next-permutation.md)
@@ -175,6 +180,7 @@ The data structures mainly includes:
175180
- [0098.validate-binary-search-tree](./problems/98.validate-binary-search-tree.md)
176181
- [0102.binary-tree-level-order-traversal](./problems/102.binary-tree-level-order-traversal.md)
177182
- [0103.binary-tree-zigzag-level-order-traversal](./problems/103.binary-tree-zigzag-level-order-traversal.md)
183+
- [105.Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal.md](./problems/105.Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal.md)
178184
- [0113.path-sum-ii](./problems/113.path-sum-ii.md)
179185
- [0129.sum-root-to-leaf-numbers](./problems/129.sum-root-to-leaf-numbers.md)
180186
- [0130.surrounded-regions](./problems/130.surrounded-regions.md)
@@ -226,6 +232,7 @@ The data structures mainly includes:
226232
- [0025.reverse-nodes-in-k-group](./problems/25.reverse-nodes-in-k-groups-en.md) 🆕
227233
- [0032.longest-valid-parentheses](./problems/32.longest-valid-parentheses.md) 🆕
228234
- [0042.trapping-rain-water](./problems/42.trapping-rain-water.md)
235+
- [0052.N-Queens-II](./problems/52.N-Queens-II.md) 🆕
229236
- [0124.binary-tree-maximum-path-sum](./problems/124.binary-tree-maximum-path-sum.md)
230237
- [0128.longest-consecutive-sequence](./problems/128.longest-consecutive-sequence.md)
231238
- [0145.binary-tree-postorder-traversal](./problems/145.binary-tree-postorder-traversal.md)

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ leetcode 题解,记录自己的 leetcode 解题之路。
124124
125125
#### 简单难度
126126

127+
- [0001.TwoSum](./problems/1.TwoSum.md) 🆕
127128
- [0020.Valid Parentheses](./problems/20.validParentheses.md)
129+
- [0021.MergeTwoSortedLists](./problems/21.MergeTwoSortedLists.md) 🆕
128130
- [0026.remove-duplicates-from-sorted-array](./problems/26.remove-duplicates-from-sorted-array.md)
129131
- [0053.maximum-sum-subarray](./problems/53.maximum-sum-subarray-cn.md) 🆕
130132
- [0088.merge-sorted-array](./problems/88.merge-sorted-array.md)
@@ -149,8 +151,9 @@ leetcode 题解,记录自己的 leetcode 解题之路。
149151
- [0283.move-zeroes](./problems/283.move-zeroes.md)
150152
- [0342.power-of-four](./problems/342.power-of-four.md)
151153
- [0349.intersection-of-two-arrays](./problems/349.intersection-of-two-arrays.md)
152-
- [0437.path-sum-iii](./problems/437.path-sum-iii.md) 🆕
153154
- [0371.sum-of-two-integers](./problems/371.sum-of-two-integers.md)
155+
- [0437.path-sum-iii](./problems/437.path-sum-iii.md) 🆕
156+
- [0455.AssignCookies](./problems/455.AssignCookies.md) 🆕
154157
- [0501.find-mode-in-binary-search-tree](./problems/501.Find-Mode-in-Binary-Search-Tree.md)🆕
155158
- [0575.distribute-candies](./problems/575.distribute-candies.md)
156159
- [0874.walking-robot-simulation](./problems/874.walking-robot-simulation.md) 🆕
@@ -164,7 +167,9 @@ leetcode 题解,记录自己的 leetcode 解题之路。
164167
- [0005.longest-palindromic-substring](./problems/5.longest-palindromic-substring.md)
165168
- [0011.container-with-most-water](./problems/11.container-with-most-water.md)
166169
- [0015.3-sum](./problems/15.3-sum.md)
170+
- [0017.Letter-Combinations-of-a-Phone-Number](./problems/17.Letter-Combinations-of-a-Phone-Number.md) 🆕
167171
- [0019. Remove Nth Node From End of List](./problems/19.removeNthNodeFromEndofList.md)
172+
- [0022.GenerateParentheses](./problems/22.GenerateParentheses.md) 🆕
168173
- [0024. Swap Nodes In Pairs](./problems/24.swapNodesInPairs.md)
169174
- [0029.divide-two-integers](./problems/29.divide-two-integers.md)
170175
- [0031.next-permutation](./problems/31.next-permutation.md)
@@ -195,6 +200,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
195200
- [0098.validate-binary-search-tree](./problems/98.validate-binary-search-tree.md)
196201
- [0102.binary-tree-level-order-traversal](./problems/102.binary-tree-level-order-traversal.md)
197202
- [0103.binary-tree-zigzag-level-order-traversal](./problems/103.binary-tree-zigzag-level-order-traversal.md)
203+
- [105.Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal.md](./problems/105.Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal.md)
198204
- [0113.path-sum-ii](./problems/113.path-sum-ii.md)
199205
- [0129.sum-root-to-leaf-numbers](./problems/129.sum-root-to-leaf-numbers.md)
200206
- [0130.surrounded-regions](./problems/130.surrounded-regions.md)
@@ -266,6 +272,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
266272
- [0030.substring-with-concatenation-of-all-words](./problems/30.substring-with-concatenation-of-all-words.md)
267273
- [0032.longest-valid-parentheses](./problems/32.longest-valid-parentheses.md)
268274
- [0042.trapping-rain-water](./problems/42.trapping-rain-water.md)
275+
- [0052.N-Queens-II](./problems/52.N-Queens-II.md) 🆕
269276
- [0084.largest-rectangle-in-histogram](./problems/84.largest-rectangle-in-histogram.md) 🆕
270277
- [0085.maximal-rectangle](./problems/85.maximal-rectangle.md)
271278
- [0124.binary-tree-maximum-path-sum](./problems/124.binary-tree-maximum-path-sum.md)

assets/problems/105.index_explain.jpg

82.7 KB
Loading

problems/1.TwoSum.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
## 题目地址
2+
https://leetcode-cn.com/problems/two-sum
3+
4+
## 题目描述
5+
```
6+
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
7+
8+
你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
9+
10+
示例:
11+
12+
给定 nums = [2, 7, 11, 15], target = 9
13+
14+
因为 nums[0] + nums[1] = 2 + 7 = 9
15+
所以返回 [0, 1]
16+
```
17+
## 思路
18+
最容易想到的就是暴力枚举,我们可以利用两层 for 循环来遍历每个元素,并查找满足条件的目标元素。不过这样时间复杂度为 O(N^2),空间复杂度为 O(1),时间复杂度较高,我们要想办法进行优化。我们可以增加一个 Map 记录已经遍历过的数字及其对应的索引值。这样当遍历一个新数字的时候去 Map 里查询,target 与该数的差值是否已经在前面的数字中出现过。如果出现过,那么已经得出答案,就不必再往下执行了。
19+
20+
## 关键点
21+
22+
- 求和转换为求差
23+
- 借助 Map 结构将数组中每个元素及其索引相互对应
24+
- 以空间换时间,将查找时间从 O(N) 降低到 O(1)
25+
26+
## 代码
27+
* 语言支持:JS
28+
29+
```js
30+
/**
31+
* @param {number[]} nums
32+
* @param {number} target
33+
* @return {number[]}
34+
*/
35+
const twoSum = function (nums, target) {
36+
const map = new Map();
37+
for (let i = 0; i < nums.length; i++) {
38+
const diff = target - nums[i];
39+
if (map.has(diff)) {
40+
return [map.get(diff), i];
41+
}
42+
map.set(nums[i], i);
43+
}
44+
}
45+
```
46+
47+
***复杂度分析***
48+
49+
- 时间复杂度:O(N)
50+
- 空间复杂度:O(N)
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
## 问题地址/Problem URL
2+
3+
https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
4+
5+
## 问题介绍/Problem Description
6+
7+
Given preorder and inorder traversal of a tree, construct the binary tree.
8+
9+
Note:
10+
You may assume that duplicates do not exist in the tree.
11+
12+
For example, given
13+
14+
```java
15+
preorder = [3,9,20,15,7]
16+
inorder = [9,3,15,20,7]
17+
```
18+
19+
Return the following binary tree:
20+
21+
```bash
22+
3
23+
/ \
24+
9 20
25+
/ \
26+
15 7
27+
```
28+
29+
## 思路/Thinking Path
30+
31+
目标是构造二叉树。
32+
33+
构造二叉树需要根的值、左子树和右子树。
34+
35+
此问题可被抽象为:从前序遍历和中序遍历中找到根节点、左子树和右子树。
36+
37+
先找根:
38+
由前序遍历的性质,第`0`个节点为当前树的根节点。
39+
再找左右子树:
40+
在中序遍历中找到这个根节点,设其下标为`i`。由中序遍历的性质,`0 ~ i-1` 是左子树的中序遍历,`i+1 ~ inorder.length-1`是右子树的中序遍历。
41+
42+
然后递归求解,终止条件是左右子树为`null`
43+
44+
We are going to construct a binary tree from its preorder and inorder traversal.
45+
46+
To build a binary tree, it requires us to creact a new `TreeNode` as the root with filling in the root value. And then, find its left child and right child recursively until the left or right child is `null`.
47+
48+
Now this problem is abstracted as how to find the root node, left child and right child from the preorder traversal and inorder traversal.
49+
50+
In preorder traversal, the first node (`preorder[0]`) is the root of current binary tree. In inorder traversal, find the location of this root which is `i`. The left sub-tree is `0 to i-1` and the right sub-tree is `i+1 to inorder.length-1` in inorder traversal.
51+
52+
Then applying the previous operation to the left and right sub-trees.
53+
54+
## 关键解析/Key Points
55+
56+
如何在前序遍历的数组里找到左右子树的:
57+
- 根据前序遍历的定义可知,每个当前数组的第一个元素就是当前子树的根节点的值
58+
- 在中序遍历数组中找到这个值,设其下标为`inRoot`
59+
- 当前中序遍历数组的起点`inStart``inRoot`之间就是左子树,其长度`leftChldLen``inRoot-inStart`
60+
- 当前中序遍历数组的终点`inEnd``inRoot`之间就是右子树
61+
- 前序遍历和中序遍历中左子树的长度是相等的,所以在前序遍历数组中,根节点下标`preStart`往后数`leftChldLen`即为左子树的最后一个节点,其下标为`preStart+leftChldLen`,右子树的第一个节点下标为`preStart+leftChldLen+1`
62+
63+
**PLEASE READ THE CODE BEFORE READING THIS PART**
64+
65+
If you can't figure out how to get the index of the left and right child, please read this.
66+
67+
- index of current node in preorder array is preStart(or whatever your call it), it's the root of a subtree.
68+
- according to the properties of preoder traversal, all right sub-tree nodes are behine all left sub-tree nodes. The length of left sub-tree can help us to divide left and right sub-trees.
69+
- the length of left sub-tree can be find in the inorder traversal. The location of current node is `inRoot`(or whatever your call it). The start index of current inorder array is `inStart`(or whatever your call it). So, the lenght of the left sub-tree is `leftChldLen = inRoot - inStart`.
70+
71+
![explain](../assets/problems/105.index_explain.jpg)
72+
73+
## 代码/Code
74+
75+
- Java
76+
77+
```java
78+
/**
79+
* Definition for a binary tree node.
80+
* public class TreeNode {
81+
* int val;
82+
* TreeNode left;
83+
* TreeNode right;
84+
* TreeNode(int x) { val = x; }
85+
* }
86+
*/
87+
class Solution {
88+
public TreeNode buildTree(int[] preorder, int[] inorder) {
89+
if (preorder.length != inorder.length) return null;
90+
91+
HashMap<Integer, Integer> map = new HashMap<> ();
92+
93+
for (int i=0; i<inorder.length; i++) {
94+
map.put(inorder[i], i);
95+
}
96+
97+
return helper(preorder, 0, preorder.length-1, inorder, 0, inorder.length-1, map);
98+
}
99+
100+
public TreeNode helper(int[] preorder, int preStart, int preEnd, int[] inorder, int inStart, int inEnd, HashMap<Integer, Integer> map) {
101+
if (preStart>preEnd || inStart>inEnd) return null;
102+
103+
TreeNode root = new TreeNode(preorder[prestart]);
104+
int inRoot = map.get(preorder[preStart]);
105+
int leftChldLen = inRoot - inStart;
106+
107+
root.left = helper(preorder, preStart+1, preStart+leftChldLen, inorder, inStart, inRoot-1, map);
108+
root.left = helper(preorder, preStart+leftChldLen+1, preEnd, inorder, inRoot+1, inEnd, map);
109+
110+
return root;
111+
}
112+
}
113+
```

problems/124.binary-tree-maximum-path-sum.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ Output: 42
7474

7575
## 代码
7676

77+
代码支持:JavaScript,Java
78+
79+
- JavaScript
80+
7781
```js
7882

7983

@@ -116,5 +120,35 @@ var maxPathSum = function(root) {
116120
};
117121
```
118122

123+
- Java
124+
125+
```java
126+
/**
127+
* Definition for a binary tree node.
128+
* public class TreeNode {
129+
* int val;
130+
* TreeNode left;
131+
* TreeNode right;
132+
* TreeNode(int x) { val = x; }
133+
* }
134+
*/
135+
class Solution {
136+
int ans;
137+
public int maxPathSum(TreeNode root) {
138+
ans = Integer.MIN_VALUE;
139+
helper(root); // recursion
140+
return ans;
141+
}
142+
143+
public int helper(TreeNode root) {
144+
if (root == null) return 0;
145+
int leftMax = Math.max(0, helper(root.left)); // find the max sub-path sum in left sub-tree
146+
int rightMax = Math.max(0, helper(root.right)); // find the max sub-path sum in right sub-tree
147+
ans = Math.max(ans, leftMax+rightMax+root.val); // find the max path sum at current node
148+
return max(leftMax, rightMax) + root.val; // according to the definition of path, the return value of current node can only be that the sum of current node value plus either left or right max path sum.
149+
}
150+
}
151+
```
152+
119153
## 相关题目
120154
- [113.path-sum-ii](./113.path-sum-ii.md)
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
## 题目地址
2+
https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number
3+
4+
## 题目描述
5+
```
6+
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。
7+
8+
给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。
9+
10+
示例:
11+
12+
输入:"23"
13+
输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
14+
15+
说明:
16+
尽管上面的答案是按字典序排列的,但是你可以任意选择答案输出的顺序。
17+
18+
```
19+
20+
## 思路
21+
使用回溯法进行求解,回溯是一种通过穷举所有可能情况来找到所有解的算法。如果一个候选解最后被发现并不是可行解,回溯算法会舍弃它,并在前面的一些步骤做出一些修改,并重新尝试找到可行解。究其本质,其实就是枚举。
22+
23+
如果没有更多的数字需要被输入,说明当前的组合已经产生。
24+
25+
如果还有数字需要被输入:
26+
- 遍历下一个数字所对应的所有映射的字母
27+
- 将当前的字母添加到组合最后,也就是 str + tmp[r]
28+
29+
## 关键点
30+
利用回溯思想解题,在for循环中调用递归。
31+
32+
## 代码
33+
* 语言支持:JS
34+
35+
```js
36+
/**
37+
* @param {string} digits
38+
* @return {string[]}
39+
*/
40+
const letterCombinations = function (digits) {
41+
if (!digits) {
42+
return [];
43+
}
44+
const len = digits.length;
45+
const map = new Map();
46+
map.set('2', 'abc');
47+
map.set('3', 'def');
48+
map.set('4', 'ghi');
49+
map.set('5', 'jkl');
50+
map.set('6', 'mno');
51+
map.set('7', 'pqrs');
52+
map.set('8', 'tuv');
53+
map.set('9', 'wxyz');
54+
const result = [];
55+
56+
function generate(i, str) {
57+
if (i == len) {
58+
result.push(str);
59+
return;
60+
}
61+
const tmp = map.get(digits[i]);
62+
for (let r = 0; r < tmp.length; r++) {
63+
generate(i + 1, str + tmp[r]);
64+
}
65+
}
66+
generate(0, '');
67+
return result;
68+
};
69+
```
70+
71+
***复杂度分析***
72+
73+
N + M 是输入数字的总数
74+
75+
- 时间复杂度:O(3^N * 4^M)
76+
- 空间复杂度:O(3^N * 4^M)
77+

0 commit comments

Comments
 (0)