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

Commit 43b094e

Browse files
Merge branch 'youngyangyang04:master' into master
2 parents 4af177b + 56045e0 commit 43b094e

6 files changed

+159
-113
lines changed

‎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
var lowestCommonAncestor = function(root, p, q) {
336-
if(root.val > p.val && root.val > q.val)
337-
return lowestCommonAncestor(root.left, p , q);
338-
else if(root.val < p.val && root.val < q.val)
339-
return lowestCommonAncestor(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
var lowestCommonAncestor = 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-
else if(root.val<p.val&&root.val<q.val)
346+
}else if(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-
return root;
353+
return null;
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
var lowestCommonAncestor = 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+
const travelTree = 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+
return travelTree(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+
class Solution:
220+
def numSquares(self, n: int) -> int:
221+
# 初始化
222+
# 组成和的完全平方数的最多个数,就是只用1构成
223+
# 因此,dp[i] = i
224+
dp = [i for i in range(n + 1)]
225+
# dp[0] = 0 无意义,只是为了方便记录特殊情况:
226+
# n本身就是完全平方数,dp[n] = min(dp[n], dp[n - n] + 1) = 1
227+
228+
for i in range(1, n): # 遍历物品
229+
if i * i > n:
230+
break
231+
num = i * i
232+
for j in range(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/0501.二叉搜索树中的众数.md

Lines changed: 66 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -523,52 +523,76 @@ func traversal(root *TreeNode,result *[]int,pre *TreeNode){//遍历统计
523523
}
524524
```
525525

526-
JavaScript版本
527-
526+
JavaScript版本:
527+
使用额外空间map的方法:
528528
```javascript
529-
/**
530-
* Definition for a binary tree node.
531-
* function TreeNode(val, left, right) {
532-
* this.val = (val===undefined ? 0 : val)
533-
* this.left = (left===undefined ? null : left)
534-
* this.right = (right===undefined ? null : right)
535-
* }
536-
*/
537-
/**
538-
* @param {TreeNode} root
539-
* @return {number[]}
540-
*/
541-
var findMode = function (root) {
542-
let maxCount = 0;
543-
let curCount = 0;
544-
let pre = null;
529+
var findMode = function(root) {
530+
// 使用递归中序遍历
531+
let map = new Map();
532+
// 1. 确定递归函数以及函数参数
533+
const traverTree = function(root) {
534+
// 2. 确定递归终止条件
535+
if(root === null) {
536+
return ;
537+
}
538+
traverTree(root.left);
539+
// 3. 单层递归逻辑
540+
map.set(root.val,map.has(root.val)?map.get(root.val)+1:1);
541+
traverTree(root.right);
542+
}
543+
traverTree(root);
544+
//上面把数据都存储到map
545+
//下面开始寻找map里面的
546+
// 定义一个最大出现次数的初始值为root.val的出现次数
547+
let maxCount = map.get(root.val);
548+
// 定义一个存放结果的数组res
545549
let res = [];
546-
const inOrder = (root) => {
547-
if (root === null)
548-
return;
549-
inOrder(root.left);
550-
551-
if (pre === null)
552-
curCount = 1;
553-
else if (pre.val === root.val)
554-
curCount++;
555-
else
556-
curCount = 1;
557-
pre = root;
558-
559-
if (curCount === maxCount)
560-
res.push(root.val);
561-
562-
if (curCount > maxCount) {
563-
maxCount = curCount;
564-
res.splice(0, res.length);
565-
res.push(root.val);
550+
for(let [key,value] of map) {
551+
// 如果当前值等于最大出现次数就直接在res增加该值
552+
if(value === maxCount) {
553+
res.push(key);
566554
}
567-
568-
inOrder(root.right);
569-
return;
555+
// 如果value的值大于原本的maxCount就清空res的所有值,因为找到了更大的
556+
if(value>maxCount) {
557+
res = [];
558+
maxCount = value;
559+
res.push(key);
560+
}
561+
}
562+
return res;
563+
};
564+
```
565+
不使用额外空间,利用二叉树性质,中序遍历(有序):
566+
```javascript
567+
var findMode = function(root) {
568+
// 不使用额外空间,使用中序遍历,设置出现最大次数初始值为1
569+
let count = 0,maxCount = 1;
570+
let pre = root,res = [];
571+
// 1.确定递归函数及函数参数
572+
const travelTree = function(cur) {
573+
// 2. 确定递归终止条件
574+
if(cur === null) {
575+
return ;
576+
}
577+
travelTree(cur.left);
578+
// 3. 单层递归逻辑
579+
if(pre.val === cur.val) {
580+
count++;
581+
}else {
582+
count = 1;
583+
}
584+
pre = cur;
585+
if(count === maxCount) {
586+
res.push(cur.val);
587+
}
588+
if(count > maxCount) {
589+
res = [];
590+
maxCount = count;
591+
res.push(cur.val);
592+
}
593+
travelTree(cur.right);
570594
}
571-
inOrder(root);
595+
travelTree(root);
572596
return res;
573597
};
574598
```

‎problems/剑指Offer58-II.左旋转字符串.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,27 @@ class Solution {
118118
}
119119
```
120120

121+
```python
122+
# 方法一:可以使用切片方法
123+
class Solution:
124+
def reverseLeftWords(self, s: str, n: int) -> str:
125+
return s[n:] + s[0:n]
126+
127+
# 方法二:也可以使用上文描述的方法,有些面试中不允许使用切片,那就使用上文作者提到的方法
128+
# class Solution:
129+
# def reverseLeftWords(self, s: str, n: int) -> str:
130+
# s = list(s)
131+
# s[0:n] = list(reversed(s[0:n]))
132+
# s[n:] = list(reversed(s[n:]))
133+
# s.reverse()
134+
135+
# return "".join(s)
136+
137+
138+
# 时间复杂度:O(n)
139+
# 空间复杂度:O(n),python的string为不可变,需要开辟同样大小的list空间来修改
140+
```
141+
121142
Go:
122143

123144
```go

‎problems/背包理论基础01背包-1.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ leetcode上没有纯01背包的问题,都是01背包应用方面的题目,
8282

8383
那么可以有两个方向推出来dp[i][j],
8484

85-
* 由dp[i - 1][j]推出,即背包容量为j,里面不放物品i的最大价值,此时dp[i][j]就是dp[i - 1][j]
85+
* 由dp[i - 1][j]推出,即背包容量为j,里面不放物品i的最大价值,此时dp[i][j]就是dp[i - 1][j]。(其实就是当物品i的重量大于背包j的重量时,物品i无法放进背包中,所以被背包内的价值依然和前面相同。)
8686
* 由dp[i - 1][j - weight[i]]推出,dp[i - 1][j - weight[i]] 为背包容量为j - weight[i]的时候不放物品i的最大价值,那么dp[i - 1][j - weight[i]] + value[i] (物品i的价值),就是背包放物品i得到的最大价值
8787

8888
所以递归公式: dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /