Skip to content

Commit d17fb9b

Browse files
authored
Merge branch 'youngyangyang04:master' into master
2 parents d1e9630 + c8149ce commit d17fb9b

19 files changed

+231
-119
lines changed

problems/0040.组合总和II.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ candidates 中的每个数字在每个组合中只能使用一次。
6363

6464
都知道组合问题可以抽象为树形结构,那么“使用过”在这个树形结构上是有两个维度的,一个维度是同一树枝上使用过,一个维度是同一树层上使用过。**没有理解这两个层面上的“使用过” 是造成大家没有彻底理解去重的根本原因。**
6565

66-
那么问题来了,我们是要同一树层上使用过,还是统一树枝上使用过呢
66+
那么问题来了,我们是要同一树层上使用过,还是同一树枝上使用过呢
6767

6868
回看一下题目,元素在同一个组合内是可以重复的,怎么重复都没事,但两个组合不能相同。
6969

problems/0051.N皇后.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,27 @@ n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并
2121
每一种解法包含一个明确的 n 皇后问题的棋子放置方案,该方案中 'Q' 和 '.' 分别代表了皇后和空位。
2222

2323
示例:
24+
2425
输入: 4
25-
输出: [
26-
[".Q..", // 解法 1
26+
27+
输出:
28+
29+
解法 1
30+
31+
[
32+
[".Q..",
2733
"...Q",
2834
"Q...",
2935
"..Q."],
3036

31-
["..Q.", // 解法 2
37+
解法 2
38+
39+
["..Q.",
3240
"Q...",
3341
"...Q",
3442
".Q.."]
3543
]
44+
3645
解释: 4 皇后问题存在两个不同的解法。
3746

3847
提示:

problems/0052.N皇后II.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,22 @@ n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并
2222
示例:
2323

2424
输入: 4
25+
2526
输出: 2
27+
2628
解释: 4 皇后问题存在如下两个不同的解法。
29+
30+
解法 1
31+
2732
[
28-
 [".Q..",  // 解法 1
33+
 [".Q..",
2934
  "...Q",
3035
  "Q...",
3136
  "..Q."],
3237

33-
 ["..Q.",  // 解法 2
38+
解法 2
39+
40+
 ["..Q.",
3441
  "Q...",
3542
  "...Q",
3643
  ".Q.."]

problems/0093.复原IP地址.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,35 @@ class Solution {
309309
```
310310

311311
python版本:
312+
```python
313+
class Solution:
314+
def restoreIpAddresses(self, s: str) -> List[str]:
315+
res = []
316+
path = [] # 存放分割后的字符
317+
# 判断数组中的数字是否合法
318+
def isValid(p):
319+
if p == '0': return True # 解决"0000"
320+
if p[0] == '0': return False
321+
if int(p) > 0 and int(p) <256: return True
322+
return False
312323

324+
def backtrack(s, startIndex):
325+
if len(s) > 12: return # 字符串长度最大为12
326+
if len(path) == 4 and startIndex == len(s): # 确保切割完,且切割后的长度为4
327+
res.append(".".join(path[:])) # 字符拼接
328+
return
329+
330+
for i in range(startIndex, len(s)):
331+
if len(s) - startIndex > 3*(4 - len(path)): continue # 剪枝,剩下的字符串大于允许的最大长度则跳过
332+
p = s[startIndex:i+1] # 分割字符
333+
if isValid(p): # 判断字符是否有效
334+
path.append(p)
335+
else: continue
336+
backtrack(s, i + 1) # 寻找i+1为起始位置的子串
337+
path.pop()
338+
backtrack(s, 0)
339+
return res
340+
```
313341
```python
314342
class Solution(object):
315343
def restoreIpAddresses(self, s):

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,6 +1528,29 @@ public:
15281528
```
15291529
15301530
Java:
1531+
```Java
1532+
class Solution {
1533+
public int maxDepth(TreeNode root) {
1534+
if (root == null) return 0;
1535+
Queue<TreeNode> que = new LinkedList<>();
1536+
que.offer(root);
1537+
int depth = 0;
1538+
while (!que.isEmpty())
1539+
{
1540+
int len = que.size();
1541+
while (len > 0)
1542+
{
1543+
TreeNode node = que.poll();
1544+
if (node.left != null) que.offer(node.left);
1545+
if (node.right != null) que.offer(node.right);
1546+
len--;
1547+
}
1548+
depth++;
1549+
}
1550+
return depth;
1551+
}
1552+
}
1553+
```
15311554

15321555

15331556
Python:

problems/0131.分割回文串.md

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,8 @@ class Solution {
292292
```
293293

294294
Python:
295-
```py
295+
```python
296+
# 版本一
296297
class Solution:
297298
def partition(self, s: str) -> List[List[str]]:
298299
res = []
@@ -310,7 +311,36 @@ class Solution:
310311
return res
311312

312313
```
313-
314+
```python
315+
# 版本二
316+
class Solution:
317+
def partition(self, s: str) -> List[List[str]]:
318+
res = []
319+
path = [] #放已经回文的子串
320+
# 双指针法判断是否是回文串
321+
def isPalindrome(s):
322+
n = len(s)
323+
i, j = 0, n - 1
324+
while i < j:
325+
if s[i] != s[j]:return False
326+
i += 1
327+
j -= 1
328+
return True
329+
330+
def backtrack(s, startIndex):
331+
if startIndex >= len(s): # 如果起始位置已经大于s的大小,说明已经找到了一组分割方案了
332+
res.append(path[:])
333+
return
334+
for i in range(startIndex, len(s)):
335+
p = s[startIndex:i+1] # 获取[startIndex,i+1]在s中的子串
336+
if isPalindrome(p): # 是回文子串
337+
path.append(p)
338+
else: continue #不是回文,跳过
339+
backtrack(s, i + 1)
340+
path.pop() #回溯过程,弹出本次已经填在path的子串
341+
backtrack(s, 0)
342+
return res
343+
```
314344
Go:
315345
> 注意切片(go切片是披着值类型外衣的引用类型)
316346

problems/0189.旋转数组.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,22 @@ class Solution:
131131
## JavaScript
132132

133133
```js
134+
var rotate = function (nums, k) {
135+
function reverse(nums, i, j) {
136+
while (i < j) {
137+
[nums[i],nums[j]] = [nums[j],nums[i]]; // 解构赋值
138+
i++;
139+
j--;
140+
}
141+
}
142+
let n = nums.length;
143+
k %= n;
144+
if (k) {
145+
reverse(nums, 0, n - 1);
146+
reverse(nums, 0, k - 1);
147+
reverse(nums, k, n - 1);
148+
}
149+
};
134150
```
135151

136152
-----------------------

problems/0491.递增子序列.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public:
184184

185185
这份代码在leetcode上提交,要比版本一耗时要好的多。
186186

187-
**所以正如在[哈希表:总结篇!(每逢总结必经典)](https://programmercarl.com/哈希表总结.html)中说的那样,数组,set,map都可以做哈希表,而且数组干的活,map和set都能干,但如何数值范围小的话能用数组尽量用数组**
187+
**所以正如在[哈希表:总结篇!(每逢总结必经典)](https://programmercarl.com/哈希表总结.html)中说的那样,数组,set,map都可以做哈希表,而且数组干的活,map和set都能干,但如果数值范围小的话能用数组尽量用数组**
188188

189189

190190

problems/剑指Offer05.替换空格.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
# 题目:剑指Offer 05.替换空格
1111

12-
https://leetcode-cn.com/problems/ti-huan-kong-ge-lcof/
12+
[力扣题目链接](https://leetcode-cn.com/problems/ti-huan-kong-ge-lcof/)
1313

1414
请实现一个函数,把字符串 s 中的每个空格替换成"%20"。
1515

@@ -81,12 +81,12 @@ public:
8181
8282
此时算上本题,我们已经做了七道双指针相关的题目了分别是:
8383
84-
* [27.移除元素](https://mp.weixin.qq.com/s/RMkulE4NIb6XsSX83ra-Ww)
85-
* [15.三数之和](https://mp.weixin.qq.com/s/QfTNEByq1YlNSXRKEumwHg)
86-
* [18.四数之和](https://mp.weixin.qq.com/s/SBU3THi1Kv6Sar7htqCB2Q)
87-
* [206.翻转链表](https://mp.weixin.qq.com/s/ckEvIVGcNLfrz6OLOMoT0A)
88-
* [142.环形链表II](https://mp.weixin.qq.com/s/gt_VH3hQTqNxyWcl1ECSbQ)
89-
* [344.反转字符串](https://mp.weixin.qq.com/s/_rNm66OJVl92gBDIbGpA3w)
84+
* [27.移除元素](https://programmercarl.com/0027.移除元素.html)
85+
* [15.三数之和](https://programmercarl.com/0015.三数之和.html)
86+
* [18.四数之和](https://programmercarl.com/0018.四数之和.html)
87+
* [206.翻转链表](https://programmercarl.com/0206.翻转链表.html)
88+
* [142.环形链表II](https://programmercarl.com/0142.环形链表II.html)
89+
* [344.反转字符串](https://programmercarl.com/0344.反转字符串.html)
9090
9191
# 拓展
9292

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@
1111
1212
# 题目:剑指Offer58-II.左旋转字符串
1313

14-
https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof/
14+
[力扣题目链接](https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof/)
1515

1616
字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如,输入字符串"abcdefg"和数字2,该函数将返回左旋转两位得到的结果"cdefgab"。
1717

18-
1918
示例 1:
2019
输入: s = "abcdefg", k = 2
2120
输出: "cdefgab"
@@ -34,7 +33,7 @@ https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof/
3433
不能使用额外空间的话,模拟在本串操作要实现左旋转字符串的功能还是有点困难的。
3534

3635

37-
那么我们可以想一下上一题目[字符串:花式反转还不够!](https://mp.weixin.qq.com/s/4j6vPFHkFAXnQhmSkq2X9g)中讲过,使用整体反转+局部反转就可以实现,反转单词顺序的目的。
36+
那么我们可以想一下上一题目[字符串:花式反转还不够!](https://programmercarl.com/0151.翻转字符串里的单词.html)中讲过,使用整体反转+局部反转就可以实现,反转单词顺序的目的。
3837

3938
这道题目也非常类似,依然可以通过局部反转+整体反转 达到左旋转的目的。
4039

@@ -76,13 +75,13 @@ public:
7675
7776
此时我们已经反转好多次字符串了,来一起回顾一下吧。
7877
79-
在这篇文章[344.反转字符串](https://mp.weixin.qq.com/s/_rNm66OJVl92gBDIbGpA3w),第一次讲到反转一个字符串应该怎么做,使用了双指针法。
78+
在这篇文章[344.反转字符串](https://programmercarl.com/0344.反转字符串.html),第一次讲到反转一个字符串应该怎么做,使用了双指针法。
8079
81-
然后发现[541. 反转字符串II](https://mp.weixin.qq.com/s/pzXt6PQ029y7bJ9YZB2mVQ),这里开始给反转加上了一些条件,当需要固定规律一段一段去处理字符串的时候,要想想在在for循环的表达式上做做文章。
80+
然后发现[541. 反转字符串II](https://programmercarl.com/0541.反转字符串II.html),这里开始给反转加上了一些条件,当需要固定规律一段一段去处理字符串的时候,要想想在在for循环的表达式上做做文章。
8281
83-
后来在[151.翻转字符串里的单词](https://mp.weixin.qq.com/s/4j6vPFHkFAXnQhmSkq2X9g)中,要对一句话里的单词顺序进行反转,发现先整体反转再局部反转 是一个很妙的思路。
82+
后来在[151.翻转字符串里的单词](https://programmercarl.com/0151.翻转字符串里的单词.html)中,要对一句话里的单词顺序进行反转,发现先整体反转再局部反转 是一个很妙的思路。
8483
85-
最后再讲到本题,本题则是先局部反转再 整体反转,与[151.翻转字符串里的单词](https://mp.weixin.qq.com/s/4j6vPFHkFAXnQhmSkq2X9g)类似,但是也是一种新的思路。
84+
最后再讲到本题,本题则是先局部反转再 整体反转,与[151.翻转字符串里的单词](https://programmercarl.com/0151.翻转字符串里的单词.html)类似,但是也是一种新的思路。
8685
8786
好了,反转字符串一共就介绍到这里,相信大家此时对反转字符串的常见操作已经很了解了。
8887

0 commit comments

Comments
 (0)