Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite339f19

Browse files
authored
Merge branch 'youngyangyang04:master' into master
2 parents1844077 +0f38eb9 commite339f19

19 files changed

+494
-226
lines changed

‎README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
>1.**介绍**:本项目是一套完整的刷题计划,旨在帮助大家少走弯路,循序渐进学算法,[关注作者](#关于作者)
44
>2.**PDF版本**[「代码随想录」算法精讲 PDF 版本](https://mp.weixin.qq.com/s/RsdcQ9umo09R6cfnwXZlrQ)
5+
>3.**刷题顺序** : README已经将刷题顺序排好了,按照顺序一道一道刷就可以。
56
>3.**学习社区** : 一起学习打卡/面试技巧/如何选择offer/大厂内推/职场规则/简历修改/技术分享/程序人生。欢迎加入[「代码随想录」学习社区](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
67
>4.**提交代码**:本项目统一使用C++语言进行讲解,但已经有Java、Python、Go、JavaScript等等多语言版本,感谢[这里的每一位贡献者](https://github.com/youngyangyang04/leetcode-master/graphs/contributors),如果你也想贡献代码点亮你的头像,[点击这里](https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A)了解提交代码的方式。
78
>5.**转载须知** :以下所有文章皆为我([程序员Carl](https://github.com/youngyangyang04))的原创。引用本项目文章请注明出处,发现恶意抄袭或搬运,会动用法律武器维护自己的权益。让我们一起维护一个良好的技术创作环境!
@@ -41,7 +42,7 @@
4142

4243
对于刷题,我们都是想用最短的时间**按照循序渐进的难度顺序把经典题目都做一遍**,这样效率才是最高的!
4344

44-
所以我整理了leetcode刷题攻略:一个超级详细的刷题顺序,**每道题目都是我精心筛选,都是经典题目高频面试题**,大家只要按照这个顺序刷就可以了,**你没看错,就是题目顺序都排好了,文章顺序就是刷题顺序!挨个刷就可以,不用自己再去题海里选题了!**
45+
所以我整理了leetcode刷题攻略:一个超级详细的刷题顺序,**每道题目都是我精心筛选,都是经典题目高频面试题**,大家只要按照这个顺序刷就可以了,**你没看错,README已经把题目顺序都排好了,文章顺序就是刷题顺序!挨个刷就可以,不用自己再去题海里选题了!**
4546

4647
而且每道题目我都写了的详细题解(图文并茂,难点配有视频),力扣上我的题解都是排在对应题目的首页,质量是有目共睹的。
4748

@@ -117,7 +118,7 @@
117118

118119
(持续更新中.....)
119120

120-
##备战秋招
121+
##知识星球精选
121122

122123
1.[选择方向的时候,我也迷茫了](https://mp.weixin.qq.com/s/ZCzFiAHZHLqHPLJQXNm75g)
123124
2.[刷题就用库函数了,怎么了?](https://mp.weixin.qq.com/s/6K3_OSaudnHGq2Ey8vqYfg)

‎problems/0062.不同路径.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,7 @@ Python:
279279
```python
280280
classSolution:# 动态规划
281281
defuniquePaths(self,m:int,n:int) ->int:
282-
dp= [[0for iinrange(n)]for jinrange(m)]
283-
for iinrange(m): dp[i][0]=1
284-
for jinrange(n): dp[0][j]=1
282+
dp= [[1for iinrange(n)]for jinrange(m)]
285283
for iinrange(1, m):
286284
for jinrange(1, n):
287285
dp[i][j]= dp[i][j-1]+ dp[i-1][j]

‎problems/0098.验证二叉搜索树.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,28 @@ func isBST(root *TreeNode, min, max int) bool {
376376
returnisBST(root.Left, min, root.Val) &&isBST(root.Right, root.Val, max)
377377
}
378378
```
379+
```go
380+
// 中序遍历解法
381+
funcisValidBST(root *TreeNode)bool {
382+
// 保存上一个指针
383+
varprev *TreeNode
384+
vartravelfunc(node *TreeNode)bool
385+
travel =func(node *TreeNode)bool {
386+
if node ==nil {
387+
returntrue
388+
}
389+
leftRes:=travel(node.Left)
390+
// 当前值小于等于前一个节点的值,返回false
391+
if prev !=nil && node.Val <= prev.Val {
392+
returnfalse
393+
}
394+
prev = node
395+
rightRes:=travel(node.Right)
396+
return leftRes && rightRes
397+
}
398+
returntravel(root)
399+
}
400+
```
379401

380402
JavaScript版本
381403

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
如下两棵树,都是这个数组的平衡二叉搜索树:
5656

57-
![108.将有序数组转换为二叉搜索树](https://code-thinking.cdn.bcebos.com/pics/108.%E5%B0%86%E6%9C%89%E5%BA%8F%E6%95%B0%E7%BB%84%E8%BD%AC%E6%8D%A2%E4%B8%BA%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91.png)
57+
![108.将有序数组转换为二叉搜索树](https://code-thinking.cdn.bcebos.com/pics/108.将有序数组转换为二叉搜索树.png)
5858

5959
如果要分割的数组长度为偶数的时候,中间元素为两个,是取左边元素 就是树1,取右边元素就是树2。
6060

‎problems/0235.二叉搜索树的最近公共祖先.md

Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -313,65 +313,49 @@ func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
313313
}
314314
```
315315

316-
317-
JavaScript版本
318-
>递归
319-
316+
JavaScript版本:
317+
1. 使用递归的方法
320318
```javascript
321-
/**
322-
* Definition for a binary tree node.
323-
* function TreeNode(val) {
324-
* this.val = val;
325-
* this.left = this.right = null;
326-
* }
327-
*/
328-
329-
/**
330-
*@param{TreeNode}root
331-
*@param{TreeNode}p
332-
*@param{TreeNode}q
333-
*@return{TreeNode}
334-
*/
335319
varlowestCommonAncestor=function(root,p,q) {
336-
if(root.val>p.val&&root.val>q.val)
337-
returnlowestCommonAncestor(root.left, p , q);
338-
elseif(root.val<p.val&&root.val<q.val)
339-
returnlowestCommonAncestor(root.right, p , q);
320+
// 使用递归的方法
321+
// 1. 使用给定的递归函数lowestCommonAncestor
322+
// 2. 确定递归终止条件
323+
if(root===null) {
324+
return root;
325+
}
326+
if(root.val>p.val&&root.val>q.val) {
327+
// 向左子树查询
328+
let left=lowestCommonAncestor(root.left,p,q);
329+
return left!==null&&left;
330+
}
331+
if(root.val<p.val&&root.val<q.val) {
332+
// 向右子树查询
333+
let right=lowestCommonAncestor(root.right,p,q);
334+
return right!==null&&right;
335+
}
340336
return root;
341337
};
342338
```
343-
344-
>迭代
345-
339+
2. 使用迭代的方法
346340
```javascript
347-
/**
348-
* Definition for a binary tree node.
349-
* function TreeNode(val) {
350-
* this.val = val;
351-
* this.left = this.right = null;
352-
* }
353-
*/
354-
355-
/**
356-
*@param{TreeNode}root
357-
*@param{TreeNode}p
358-
*@param{TreeNode}q
359-
*@return{TreeNode}
360-
*/
361341
varlowestCommonAncestor=function(root,p,q) {
362-
while(1) {
363-
if(root.val>p.val&&root.val>q.val)
342+
// 使用迭代的方法
343+
while(root) {
344+
if(root.val>p.val&&root.val>q.val) {
364345
root=root.left;
365-
elseif(root.val<p.val&&root.val<q.val)
346+
}elseif(root.val<p.val&&root.val<q.val) {
366347
root=root.right;
367-
else
368-
break;
348+
}else {
349+
return root;
350+
}
351+
369352
}
370-
returnroot;
353+
returnnull;
371354
};
372355
```
373356

374357

358+
375359
-----------------------
376360
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
377361
* B站视频:[代码随想录](https://space.bilibili.com/525438321)

‎problems/0236.二叉树的最近公共祖先.md

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -311,35 +311,34 @@ func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
311311
}
312312
```
313313

314-
JavaScript版本
315-
314+
JavaScript版本:
316315
```javascript
317-
/**
318-
* Definition for a binary tree node.
319-
* function TreeNode(val) {
320-
* this.val = val;
321-
* this.left = this.right = null;
322-
* }
323-
*/
324-
/**
325-
*@param{TreeNode}root
326-
*@param{TreeNode}p
327-
*@param{TreeNode}q
328-
*@return{TreeNode}
329-
*/
330316
varlowestCommonAncestor=function(root,p,q) {
331-
if(root=== p|| root=== q|| root===null)
332-
return root;
333-
let left=lowestCommonAncestor(root.left, p , q);
334-
let right=lowestCommonAncestor(root.right, p, q);
335-
if(left&& right)
336-
return root;
337-
if(!left)
338-
return right;
339-
return left;
317+
// 使用递归的方法
318+
// 需要从下到上,所以使用后序遍历
319+
// 1. 确定递归的函数
320+
consttravelTree=function(root,p,q) {
321+
// 2. 确定递归终止条件
322+
if(root===null|| root=== p||root=== q) {
323+
return root;
324+
}
325+
// 3. 确定递归单层逻辑
326+
let left=travelTree(root.left,p,q);
327+
let right=travelTree(root.right,p,q);
328+
if(left!==null&&right!==null) {
329+
return root;
330+
}
331+
if(left===null) {
332+
return right;
333+
}
334+
return left;
335+
}
336+
returntravelTree(root,p,q);
340337
};
341338
```
342339

340+
341+
343342
-----------------------
344343
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
345344
* B站视频:[代码随想录](https://space.bilibili.com/525438321)

‎problems/0279.完全平方数.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,26 @@ class Solution:
214214
return dp[n]
215215
```
216216

217+
Python3:
218+
```python
219+
classSolution:
220+
defnumSquares(self,n:int) ->int:
221+
# 初始化
222+
# 组成和的完全平方数的最多个数,就是只用1构成
223+
# 因此,dp[i] = i
224+
dp= [ifor iinrange(n+1)]
225+
# dp[0] = 0 无意义,只是为了方便记录特殊情况:
226+
# n本身就是完全平方数,dp[n] = min(dp[n], dp[n - n] + 1) = 1
227+
228+
for iinrange(1, n):# 遍历物品
229+
if i* i> n:
230+
break
231+
num= i* i
232+
for jinrange(num, n+1):# 遍历背包
233+
dp[j]=min(dp[j], dp[j- num]+1)
217234

218-
235+
return dp[n]
236+
```
219237

220238
Go:
221239
```go

‎problems/0416.分割等和子集.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,26 @@ class Solution:
240240
Go:
241241

242242

243+
#"diff-4dc584a7f425992e8fd17974c839c25f9679d226055a9716342b2899bf9e081f-242-244-0" data-selected="false" role="gridcell" tabindex="-1" valign="top">
244+
245+
```js
246+
varcanPartition=function(nums) {
247+
constsum= (nums.reduce((p,v)=> p+ v));
248+
if (sum&1)returnfalse;
249+
constdp=Array(sum/2+1).fill(0);
250+
for(let i=0; i<nums.length; i++) {
251+
for(let j= sum/2; j>= nums[i]; j--) {
252+
dp[j]=Math.max(dp[j], dp[j- nums[i]]+ nums[i]);
253+
if (dp[j]=== sum/2) {
254+
returntrue;
255+
}
256+
}
257+
}
258+
return dp[sum/2]=== sum/2;
259+
};
260+
```
261+
262+
243263

244264

245265
-----------------------

‎problems/0450.删除二叉搜索树中的节点.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ if (root == nullptr) return root;
6767
第五种情况有点难以理解,看下面动画:
6868

6969
![450.删除二叉搜索树中的节点](https://tva1.sinaimg.cn/large/008eGmZEly1gnbj3k596mg30dq0aigyz.gif)
70-
<imgsrc='../video/450.删除二叉搜索树中的节点.gif'width=600> </img></div>
7170

7271
动画中颗二叉搜索树中,删除元素7, 那么删除节点(元素7)的左孩子就是5,删除节点(元素7)的右子树的最左面节点是元素8。
7372

@@ -359,6 +358,51 @@ func deleteNode1(root *TreeNode)*TreeNode{
359358
}
360359
```
361360

361+
JavaScript版本
362+
363+
> 递归
364+
365+
```javascript
366+
/**
367+
* Definition for a binary tree node.
368+
* function TreeNode(val, left, right) {
369+
* this.val = (val===undefined ? 0 : val)
370+
* this.left = (left===undefined ? null : left)
371+
* this.right = (right===undefined ? null : right)
372+
* }
373+
*/
374+
/**
375+
* @param {TreeNode} root
376+
* @param {number} key
377+
* @return {TreeNode}
378+
*/
379+
var deleteNode = function (root, key) {
380+
if (root === null)
381+
return root;
382+
if (root.val === key) {
383+
if (!root.left)
384+
return root.right;
385+
else if (!root.right)
386+
return root.left;
387+
else {
388+
let cur = root.right;
389+
while (cur.left) {
390+
cur = cur.left;
391+
}
392+
cur.left = root.left;
393+
let temp = root;
394+
root = root.right;
395+
delete root;
396+
return root;
397+
}
398+
}
399+
if (root.val > key)
400+
root.left = deleteNode(root.left, key);
401+
if (root.val < key)
402+
root.right = deleteNode(root.right, key);
403+
return root;
404+
};
405+
```
362406

363407

364408

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp