Skip to content

Commit 112832c

Browse files
committed
Merge branch 'master' of github.com:flames519/leetcode-master
2 parents 3be6a9d + 6bdfab2 commit 112832c

12 files changed

+479
-20
lines changed

problems/0015.三数之和.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,33 @@ class Solution {
218218
```
219219

220220
Python:
221-
222-
221+
```Python
222+
class Solution:
223+
def threeSum(self, nums):
224+
ans = []
225+
n = len(nums)
226+
nums.sort()
227+
for i in range(n):
228+
left = i + 1
229+
right = n - 1
230+
if nums[i] > 0:
231+
break
232+
if i >= 1 and nums[i] == nums[i - 1]:
233+
continue
234+
while left < right:
235+
total = nums[i] + nums[left] + nums[right]
236+
if total > 0:
237+
right -= 1
238+
elif total < 0:
239+
left += 1
240+
else:
241+
ans.append([nums[i], nums[left], nums[right]])
242+
while left != right and nums[left] == nums[left + 1]: left += 1
243+
while left != right and nums[right] == nums[right - 1]: right -= 1
244+
left += 1
245+
right -= 1
246+
return ans
247+
```
223248
Go:
224249
```Go
225250
func threeSum(nums []int)[][]int{

problems/0056.合并区间.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,21 @@ class Solution {
168168
```
169169

170170
Python:
171-
171+
```python
172+
class Solution:
173+
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
174+
if len(intervals) == 0: return intervals
175+
intervals.sort(key=lambda x: x[0])
176+
result = []
177+
result.append(intervals[0])
178+
for i in range(1, len(intervals)):
179+
last = result[-1]
180+
if last[1] >= intervals[i][0]:
181+
result[-1] = [last[0], max(last[1], intervals[i][1])]
182+
else:
183+
result.append(intervals[i])
184+
return result
185+
```
172186

173187
Go:
174188

@@ -179,4 +193,4 @@ Go:
179193
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
180194
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
181195
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
182-
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
196+
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>

problems/0062.不同路径.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,24 @@ Python:
249249
250250
251251
Go:
252-
252+
```Go
253+
func uniquePaths(m int, n int) int {
254+
dp := make([][]int, m)
255+
for i := range dp {
256+
dp[i] = make([]int, n)
257+
dp[i][0] = 1
258+
}
259+
for j := 0; j < n; j++ {
260+
dp[0][j] = 1
261+
}
262+
for i := 1; i < m; i++ {
263+
for j := 1; j < n; j++ {
264+
dp[i][j] = dp[i-1][j] + dp[i][j-1]
265+
}
266+
}
267+
return dp[m-1][n-1]
268+
}
269+
```
253270

254271

255272

problems/0102.二叉树的层序遍历.md

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,35 @@ public:
7979
return result;
8080
}
8181
};
82+
```
83+
javascript代码:
84+
85+
```javascript
86+
var levelOrder = function(root) {
87+
//二叉树的层序遍历
88+
let res=[],queue=[];
89+
queue.push(root);
90+
if(root===null){
91+
return res;
92+
}
93+
while(queue.length!==0){
94+
// 记录当前层级节点数
95+
let length=queue.length;
96+
//存放每一层的节点
97+
let curLevel=[];
98+
for(let i=0;i<length;i++){
99+
let node=queue.shift();
100+
curLevel.push(node.val);
101+
// 存放当前层下一层的节点
102+
node.left&&queue.push(node.left);
103+
node.right&&queue.push(node.right);
104+
}
105+
//把每一层的结果放到结果数组
106+
res.push(curLevel);
107+
}
108+
return res;
109+
};
110+
82111
```
83112

84113
**此时我们就掌握了二叉树的层序遍历了,那么如下五道leetcode上的题目,只需要修改模板的一两行代码(不能再多了),便可打倒!**
@@ -122,6 +151,30 @@ public:
122151
}
123152
};
124153
```
154+
javascript代码
155+
156+
```javascript
157+
var levelOrderBottom = function(root) {
158+
let res=[],queue=[];
159+
queue.push(root);
160+
while(queue.length&&root!==null){
161+
// 存放当前层级节点数组
162+
let curLevel=[];
163+
// 计算当前层级节点数量
164+
let length=queue.length;
165+
while(length--){
166+
let node=queue.shift();
167+
// 把当前层节点存入curLevel数组
168+
curLevel.push(node.val);
169+
// 把下一层级的左右节点存入queue队列
170+
node.left&&queue.push(node.left);
171+
node.right&&queue.push(node.right);
172+
}
173+
res.push(curLevel);
174+
}
175+
return res.reverse();
176+
};
177+
```
125178

126179

127180
## 199.二叉树的右视图
@@ -159,6 +212,29 @@ public:
159212
}
160213
};
161214
```
215+
javascript代码:
216+
217+
```javascript
218+
var rightSideView = function(root) {
219+
//二叉树右视图 只需要把每一层最后一个节点存储到res数组
220+
let res=[],queue=[];
221+
queue.push(root);
222+
while(queue.length&&root!==null){
223+
// 记录当前层级节点个数
224+
let length=queue.length;
225+
while(length--){
226+
let node=queue.shift();
227+
//length长度为0的时候表明到了层级最后一个节点
228+
if(!length){
229+
res.push(node.val);
230+
}
231+
node.left&&queue.push(node.left);
232+
node.right&&queue.push(node.right);
233+
}
234+
}
235+
return res;
236+
};
237+
```
162238

163239
## 637.二叉树的层平均值
164240

@@ -199,6 +275,31 @@ public:
199275

200276
```
201277
278+
javascript代码:
279+
280+
```javascript
281+
var averageOfLevels = function(root) {
282+
//层级平均值
283+
let res=[],queue=[];
284+
queue.push(root);
285+
while(queue.length&&root!==null){
286+
//每一层节点个数
287+
let length=queue.length;
288+
//sum记录每一层的和
289+
let sum=0;
290+
for(let i=0;i<length;i++){
291+
let node=queue.shift();
292+
sum+=node.val;
293+
node.left&&queue.push(node.left);
294+
node.right&&queue.push(node.right);
295+
}
296+
//每一层的平均值存入数组res
297+
res.push(sum/length);
298+
}
299+
return res;
300+
};
301+
```
302+
202303
## 429.N叉树的层序遍历
203304

204305
题目链接:https://leetcode-cn.com/problems/n-ary-tree-level-order-traversal/
@@ -250,6 +351,31 @@ public:
250351
};
251352
```
252353

354+
JavaScript代码:
355+
```JavaScript
356+
var levelOrder = function(root) {
357+
//每一层可能有2个以上,所以不再使用node.left node.right
358+
let res=[],queue=[];
359+
queue.push(root);
360+
while(queue.length&&root!==null){
361+
//记录每一层节点个数还是和二叉树一致
362+
let length=queue.length;
363+
//存放每层节点 也和二叉树一致
364+
let curLevel=[];
365+
while(length--){
366+
let node = queue.shift();
367+
curLevel.push(node.val);
368+
//这里不再是 ndoe.left node.right 而是循坏node.children
369+
for(let item of node.children){
370+
item&&queue.push(item);
371+
}
372+
}
373+
res.push(curLevel);
374+
}
375+
return res;
376+
};
377+
```
378+
253379
## 515.在每个树行中找最大值
254380

255381
题目链接:https://leetcode-cn.com/problems/find-largest-value-in-each-tree-row/
@@ -287,6 +413,29 @@ public:
287413
}
288414
};
289415
```
416+
javascript代码:
417+
418+
```javascript
419+
var largestValues = function(root) {
420+
//使用层序遍历
421+
let res=[],queue=[];
422+
queue.push(root);
423+
while(root!==null&&queue.length){
424+
//设置max初始值就是队列的第一个元素
425+
let max=queue[0];
426+
let length=queue.length;
427+
while(length--){
428+
let node = queue.shift();
429+
max=max>node.val?max:node.val;
430+
node.left&&queue.push(node.left);
431+
node.right&&queue.push(node.right);
432+
}
433+
//把每一层的最大值放到res数组
434+
res.push(max);
435+
}
436+
return res;
437+
};
438+
```
290439

291440
## 116.填充每个节点的下一个右侧节点指针
292441

problems/0108.将有序数组转换为二叉搜索树.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,27 @@ class Solution {
233233
```
234234

235235
Python:
236-
236+
```python3
237+
# Definition for a binary tree node.
238+
# class TreeNode:
239+
# def __init__(self, val=0, left=None, right=None):
240+
# self.val = val
241+
# self.left = left
242+
# self.right = right
243+
#递归法
244+
class Solution:
245+
def sortedArrayToBST(self, nums: List[int]) -> TreeNode:
246+
def buildaTree(left,right):
247+
if left > right: return None #左闭右闭的区间,当区间 left > right的时候,就是空节点,当left = right的时候,不为空
248+
mid = left + (right - left) // 2 #保证数据不会越界
249+
val = nums[mid]
250+
root = TreeNode(val)
251+
root.left = buildaTree(left,mid - 1)
252+
root.right = buildaTree(mid + 1,right)
253+
return root
254+
root = buildaTree(0,len(nums) - 1) #左闭右闭区间
255+
return root
256+
```
237257

238258
Go:
239259

@@ -244,4 +264,4 @@ Go:
244264
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
245265
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
246266
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
247-
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
267+
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>

problems/0209.长度最小的子数组.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,30 @@ Python:
172172

173173

174174
Go:
175+
```go
176+
func minSubArrayLen(target int, nums []int) int {
177+
i := 0
178+
l := len(nums) // 数组长度
179+
sum := 0 // 子数组之和
180+
result := l + 1 // 初始化返回长度为l+1,目的是为了判断“不存在符合条件的子数组,返回0”的情况
181+
for j := 0; j < l; j++ {
182+
sum += nums[j]
183+
for sum >= target {
184+
subLength := j - i + 1
185+
if subLength < result {
186+
result = subLength
187+
}
188+
sum -= nums[i]
189+
i++
190+
}
191+
}
192+
if result == l+1 {
193+
return 0
194+
} else {
195+
return result
196+
}
197+
}
198+
```
175199

176200

177201
JavaScript:
@@ -200,4 +224,4 @@ var minSubArrayLen = function(target, nums) {
200224
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
201225
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
202226
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
203-
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
227+
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>

problems/0538.把二叉搜索树转换为累加树.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,26 @@ class Solution {
196196
```
197197

198198
Python:
199-
200-
199+
```python3
200+
# Definition for a binary tree node.
201+
# class TreeNode:
202+
# def __init__(self, val=0, left=None, right=None):
203+
# self.val = val
204+
# self.left = left
205+
# self.right = right
206+
#递归法
207+
class Solution:
208+
def convertBST(self, root: TreeNode) -> TreeNode:
209+
def buildalist(root):
210+
if not root: return None
211+
buildalist(root.right) #右中左遍历
212+
root.val += self.pre
213+
self.pre = root.val
214+
buildalist(root.left)
215+
self.pre = 0 #记录前一个节点的数值
216+
buildalist(root)
217+
return root
218+
```
201219
Go:
202220

203221

@@ -207,4 +225,4 @@ Go:
207225
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
208226
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
209227
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
210-
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
228+
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>

0 commit comments

Comments
 (0)