Skip to content

Commit dbc93eb

Browse files
Update
2 parents ba20624 + aab47d3 commit dbc93eb

21 files changed

+753
-10
lines changed

problems/0053.最大子序和.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ class Solution:
214214
return result
215215

216216
```
217+
贪心法
217218
```python
218219
class Solution:
219220
def maxSubArray(self, nums):
@@ -226,8 +227,18 @@ class Solution:
226227
if count <= 0: # 相当于重置最大子序起始位置,因为遇到负数一定是拉低总和
227228
count = 0
228229
return result
229-
230-
230+
```
231+
动态规划
232+
```python
233+
class Solution:
234+
def maxSubArray(self, nums: List[int]) -> int:
235+
dp = [0] * len(nums)
236+
dp[0] = nums[0]
237+
res = nums[0]
238+
for i in range(1, len(nums)):
239+
dp[i] = max(dp[i-1] + nums[i], nums[i])
240+
res = max(res, dp[i])
241+
return res
231242
```
232243
### Go
233244
贪心法

problems/0070.爬楼梯完全背包版本.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,16 @@ if __name__ == '__main__':
184184

185185
### Go:
186186
```go
187+
package main
188+
189+
import (
190+
"bufio"
191+
"fmt"
192+
"os"
193+
"strconv"
194+
"strings"
195+
)
196+
187197
func climbStairs(n int, m int) int {
188198
dp := make([]int, n+1)
189199
dp[0] = 1

problems/0093.复原IP地址.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,9 +467,37 @@ class Solution:
467467
num = int(s[start:end+1])
468468
return 0 <= num <= 255
469469

470+
回溯(版本三)
470471

471-
472-
472+
```python
473+
class Solution:
474+
def restoreIpAddresses(self, s: str) -> List[str]:
475+
result = []
476+
self.backtracking(s, 0, [], result)
477+
return result
478+
479+
def backtracking(self, s, startIndex, path, result):
480+
if startIndex == len(s):
481+
result.append('.'.join(path[:]))
482+
return
483+
484+
for i in range(startIndex, min(startIndex+3, len(s))):
485+
# 如果 i 往后遍历了,并且当前地址的第一个元素是 0 ,就直接退出
486+
if i > startIndex and s[startIndex] == '0':
487+
break
488+
# 比如 s 长度为 5,当前遍历到 i = 3 这个元素
489+
# 因为还没有执行任何操作,所以此时剩下的元素数量就是 5 - 3 = 2 ,即包括当前的 i 本身
490+
# path 里面是当前包含的子串,所以有几个元素就表示储存了几个地址
491+
# 所以 (4 - len(path)) * 3 表示当前路径至多能存放的元素个数
492+
# 4 - len(path) 表示至少要存放的元素个数
493+
if (4 - len(path)) * 3 < len(s) - i or 4 - len(path) > len(s) - i:
494+
break
495+
if i - startIndex == 2:
496+
if not int(s[startIndex:i+1]) <= 255:
497+
break
498+
path.append(s[startIndex:i+1])
499+
self.backtracking(s, i+1, path, result)
500+
path.pop()
473501
```
474502

475503
### Go

problems/0110.平衡二叉树.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -609,10 +609,13 @@ class Solution:
609609
while stack:
610610
node = stack.pop()
611611
if node:
612-
stack.append(node)
612+
stack.append(node) #
613613
stack.append(None)
614-
if node.left: stack.append(node.left)
615-
if node.right: stack.append(node.right)
614+
# 采用数组进行迭代,先将右节点加入,保证左节点能够先出栈
615+
if node.right: #
616+
stack.append(node.right)
617+
if node.left: #
618+
stack.append(node.left)
616619
else:
617620
real_node = stack.pop()
618621
left, right = height_map.get(real_node.left, 0), height_map.get(real_node.right, 0)

problems/0134.加油站.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ i从0开始累加rest[i],和记为curSum,一旦curSum小于零,说明[0, i
158158

159159
如果 curSum<0 说明 区间和1 + 区间和2 < 0, 那么 假设从上图中的位置开始计数curSum不会小于0的话,就是 区间和2>0。
160160

161-
区间和1 + 区间和2 < 0 同时 区间和2>0,只能说明区间和1 < 0, 那么就会从假设的箭头初就开始从新选择其实位置了
161+
区间和1 + 区间和2 < 0 同时 区间和2>0,只能说明区间和1 < 0, 那么就会从假设的箭头初就开始从新选择起始位置了
162162

163163

164164
**那么局部最优:当前累加rest[i]的和curSum一旦小于0,起始位置至少要是i+1,因为从i之前开始一定不行。全局最优:找到可以跑一圈的起始位置**

problems/0151.翻转字符串里的单词.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,45 @@ class Solution:
475475
words = words[::-1] # 反转单词
476476
return ' '.join(words) #列表转换成字符串
477477
```
478+
(版本四) 将字符串转换为列表后,使用双指针去除空格
479+
```python
480+
class Solution:
481+
def single_reverse(self, s, start: int, end: int):
482+
while start < end:
483+
s[start], s[end] = s[end], s[start]
484+
start += 1
485+
end -= 1
478486

487+
def reverseWords(self, s: str) -> str:
488+
result = ""
489+
fast = 0
490+
# 1. 首先将原字符串反转并且除掉空格, 并且加入到新的字符串当中
491+
# 由于Python字符串的不可变性,因此只能转换为列表进行处理
492+
s = list(s)
493+
s.reverse()
494+
while fast < len(s):
495+
if s[fast] != " ":
496+
if len(result) != 0:
497+
result += " "
498+
while s[fast] != " " and fast < len(s):
499+
result += s[fast]
500+
fast += 1
501+
else:
502+
fast += 1
503+
# 2.其次将每个单词进行翻转操作
504+
slow = 0
505+
fast = 0
506+
result = list(result)
507+
while fast <= len(result):
508+
if fast == len(result) or result[fast] == " ":
509+
self.single_reverse(result, slow, fast - 1)
510+
slow = fast + 1
511+
fast += 1
512+
else:
513+
fast += 1
514+
515+
return "".join(result)
516+
```
479517
### Go:
480518

481519
版本一:

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,11 @@ impl Solution {
454454
p: Option<Rc<RefCell<TreeNode>>>,
455455
q: Option<Rc<RefCell<TreeNode>>>,
456456
) -> Option<Rc<RefCell<TreeNode>>> {
457-
if root == p || root == q || root.is_none() {
457+
if root.is_none() {
458+
return root;
459+
}
460+
if Rc::ptr_eq(root.as_ref().unwrap(), p.as_ref().unwrap())
461+
|| Rc::ptr_eq(root.as_ref().unwrap(), q.as_ref().unwrap()) {
458462
return root;
459463
}
460464
let left = Self::lowest_common_ancestor(

problems/0300.最长上升子序列.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,29 @@ pub fn length_of_lis(nums: Vec<i32>) -> i32 {
337337
}
338338
```
339339

340+
### Cangjie:
341+
342+
```cangjie
343+
func lengthOfLIS(nums: Array<Int64>): Int64 {
344+
let n = nums.size
345+
if (n <= 1) {
346+
return n
347+
}
348+
349+
let dp = Array(n, item: 1)
350+
var res = 0
351+
for (i in 1..n) {
352+
for (j in 0..i) {
353+
if (nums[i] > nums[j]) {
354+
dp[i] = max(dp[i], dp[j] + 1)
355+
}
356+
}
357+
res = max(dp[i], res)
358+
}
359+
return res
360+
}
361+
```
362+
340363

341364
<p align="center">
342365
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

problems/0343.整数拆分.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,29 @@ class Solution {
243243
}
244244
}
245245
```
246+
贪心
247+
```Java
248+
class Solution {
249+
public int integerBreak(int n) {
250+
// with 贪心
251+
// 通过数学原理拆出更多的3乘积越大,则
252+
/**
253+
@Param: an int, the integer we need to break.
254+
@Return: an int, the maximum integer after breaking
255+
@Method: Using math principle to solve this problem
256+
@Time complexity: O(1)
257+
**/
258+
if(n == 2) return 1;
259+
if(n == 3) return 2;
260+
int result = 1;
261+
while(n > 4) {
262+
n-=3;
263+
result *=3;
264+
}
265+
return result*n;
266+
}
267+
}
268+
```
246269

247270
### Python
248271
动态规划(版本一)

problems/0459.重复的子字符串.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ KMP算法中next数组为什么遇到字符不匹配的时候可以找到上一
177177
178178
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240913110841.png)
179179
180-
这里有录友就想:如果字符串s 是有是有最小重复子串p组成,最长相等前后缀就不能更长一些? 例如这样:
180+
这里有录友就想:如果字符串s 是由最小重复子串p组成,最长相等前后缀就不能更长一些? 例如这样:
181181
182182
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240913114348.png)
183183
@@ -884,3 +884,4 @@ public int[] GetNext(string s)
884884
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
885885
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
886886
</a>
887+

0 commit comments

Comments
 (0)