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 d22464f

Browse files
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
2 parents 419db36 + 15c26d4 commit d22464f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1473
-329
lines changed

‎.DS_Store‎

-6 KB
Binary file not shown.

‎.gitignore‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.idea/
2+
.DS_Store
3+
.vscode
4+
.temp
5+
.cache
6+
*.iml
7+
__pycache__

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@
399399
24. [图论:Bellman_ford 算法](./problems/kamacoder/0094.城市间货物运输I.md)
400400
25. [图论:Bellman_ford 队列优化算法(又名SPFA)](./problems/kamacoder/0094.城市间货物运输I-SPFA.md)
401401
26. [图论:Bellman_ford之判断负权回路](./problems/kamacoder/0095.城市间货物运输II.md)
402-
27. [图论:Bellman_ford之单源有限最短路](./problems/kamacoder/0095.城市间货物运输II.md)
402+
27. [图论:Bellman_ford之单源有限最短路](./problems/kamacoder/0096.城市间货物运输III.md)
403403
28. [图论:Floyd 算法](./problems/kamacoder/0097.小明逛公园.md)
404404
29. [图论:A * 算法](./problems/kamacoder/0126.骑士的攻击astar.md)
405405
30. [图论:最短路算法总结篇](./problems/kamacoder/最短路问题总结篇.md)

‎problems/0005.最长回文子串.md‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,60 @@ public:
256256
* 时间复杂度:O(n^2)
257257
* 空间复杂度:O(1)
258258

259+
### Manacher 算法
259260

261+
Manacher 算法的关键在于高效利用回文的对称性,通过插入分隔符和维护中心、边界等信息,在线性时间内找到最长回文子串。这种方法避免了重复计算,是处理回文问题的最优解。
262+
263+
```c++
264+
//Manacher 算法
265+
class Solution {
266+
public:
267+
string longestPalindrome(string s) {
268+
// 预处理字符串,在每个字符之间插入 '#'
269+
string t = "#";
270+
for (char c : s) {
271+
t += c; // 添加字符
272+
t += '#';// 添加分隔符
273+
}
274+
int n = t.size();// 新字符串的长度
275+
vector<int> p(n, 0);// p[i] 表示以 t[i] 为中心的回文半径
276+
int center = 0, right = 0;// 当前回文的中心和右边界
277+
278+
279+
// 遍历预处理后的字符串
280+
for (int i = 0; i < n; i++) {
281+
// 如果当前索引在右边界内,利用对称性初始化 p[i]
282+
if (i < right) {
283+
p[i] = min(right - i, p[2 * center - i]);
284+
}
285+
// 尝试扩展回文
286+
while (i - p[i] - 1 >= 0 && i + p[i] + 1 < n && t[i - p[i] - 1] == t[i + p[i] + 1]) {
287+
p[i]++;// 增加回文半径
288+
}
289+
// 如果当前回文扩展超出右边界,更新中心和右边界
290+
if (i + p[i] > right) {
291+
center = i;// 更新中心
292+
right = i + p[i];// 更新右边界
293+
}
294+
}
295+
// 找到最大回文半径和对应的中心
296+
int maxLen = 0, centerIndex = 0;
297+
for (int i = 0; i < n; i++) {
298+
if (p[i] > maxLen) {
299+
maxLen = p[i];// 更新最大回文长度
300+
centerIndex = i;// 更新中心索引
301+
}
302+
}
303+
// 计算原字符串中回文子串的起始位置并返回
304+
return s.substr((centerIndex - maxLen) / 2, maxLen);
305+
}
306+
};
307+
```
308+
309+
310+
311+
* 时间复杂度:O(n)
312+
* 空间复杂度:O(n)
260313

261314
## 其他语言版本
262315

@@ -682,3 +735,4 @@ public class Solution {
682735
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
683736
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
684737
</a>
738+

‎problems/0046.全排列.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ class Solution {
201201
public void backtrack(int[] nums, LinkedList<Integer> path) {
202202
if (path.size() == nums.length) {
203203
result.add(new ArrayList<>(path));
204+
return;
204205
}
205206
for (int i =0; i < nums.length; i++) {
206207
// 如果path中已有,则跳过
@@ -524,3 +525,4 @@ public class Solution
524525
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
525526
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
526527
</a>
528+

‎problems/0053.最大子序和.md‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,42 @@ class Solution:
240240
res = max(res, dp[i])
241241
return res
242242
```
243+
244+
动态规划
245+
246+
```python
247+
class Solution:
248+
def maxSubArray(self, nums):
249+
if not nums:
250+
return 0
251+
dp = [0] * len(nums) # dp[i]表示包括i之前的最大连续子序列和
252+
dp[0] = nums[0]
253+
result = dp[0]
254+
for i in range(1, len(nums)):
255+
dp[i] = max(dp[i-1]+nums[i], nums[i]) # 状态转移公式
256+
if dp[i] > result:
257+
result = dp[i] # result 保存dp[i]的最大值
258+
return result
259+
```
260+
261+
动态规划优化
262+
263+
```python
264+
class Solution:
265+
def maxSubArray(self, nums: List[int]) -> int:
266+
max_sum = float("-inf") # 初始化结果为负无穷大,方便比较取最大值
267+
current_sum = 0 # 初始化当前连续和
268+
269+
for num in nums:
270+
271+
# 更新当前连续和
272+
# 如果原本的连续和加上当前数字之后没有当前数字大,说明原本的连续和是负数,那么就直接从当前数字开始重新计算连续和
273+
current_sum = max(current_sum+num, num)
274+
max_sum = max(max_sum, current_sum) # 更新结果
275+
276+
return max_sum
277+
```
278+
243279
### Go
244280
贪心法
245281
```go

‎problems/0055.跳跃游戏.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,23 @@ class Solution:
143143
return False
144144
```
145145

146+
```python
147+
## 基于当前最远可到达位置判断
148+
class Solution:
149+
def canJump(self, nums: List[int]) -> bool:
150+
far = nums[0]
151+
for i in range(len(nums)):
152+
# 要考虑两个情况
153+
# 1. i <= far - 表示 当前位置i 可以到达
154+
# 2. i > far - 表示 当前位置i 无法到达
155+
if i > far:
156+
return False
157+
far = max(far, nums[i]+i)
158+
# 如果循环正常结束,表示最后一个位置也可以到达,否则会在中途直接退出
159+
# 关键点在于,要想明白其实列表中的每个位置都是需要验证能否到达的
160+
return True
161+
```
162+
146163
### Go
147164

148165
```go

‎problems/0112.路径总和.md‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -564,10 +564,10 @@ class Solution:
564564

565565
return False
566566

567-
def hasPathSum(self, root: TreeNode, sum: int) -> bool:
567+
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
568568
if root is None:
569569
return False
570-
return self.traversal(root, sum - root.val)
570+
return self.traversal(root, targetSum - root.val)
571571
```
572572

573573
(版本二) 递归 + 精简
@@ -579,12 +579,12 @@ class Solution:
579579
# self.left = left
580580
# self.right = right
581581
class Solution:
582-
def hasPathSum(self, root: TreeNode, sum: int) -> bool:
582+
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
583583
if not root:
584584
return False
585-
if not root.left and not root.right and sum == root.val:
585+
if not root.left and not root.right and targetSum == root.val:
586586
return True
587-
return self.hasPathSum(root.left, sum - root.val) or self.hasPathSum(root.right, sum - root.val)
587+
return self.hasPathSum(root.left, targetSum - root.val) or self.hasPathSum(root.right, targetSum - root.val)
588588

589589
```
590590
(版本三) 迭代
@@ -596,7 +596,7 @@ class Solution:
596596
# self.left = left
597597
# self.right = right
598598
class Solution:
599-
def hasPathSum(self, root: TreeNode, sum: int) -> bool:
599+
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
600600
if not root:
601601
return False
602602
# 此时栈里要放的是pair<节点指针,路径数值>
@@ -659,13 +659,13 @@ class Solution:
659659

660660
return
661661

662-
def pathSum(self, root: TreeNode, sum: int) -> List[List[int]]:
662+
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
663663
self.result.clear()
664664
self.path.clear()
665665
if not root:
666666
return self.result
667667
self.path.append(root.val) # 把根节点放进路径
668-
self.traversal(root, sum - root.val)
668+
self.traversal(root, targetSum - root.val)
669669
return self.result
670670
```
671671

@@ -678,7 +678,7 @@ class Solution:
678678
# self.left = left
679679
# self.right = right
680680
class Solution:
681-
def pathSum(self, root: TreeNode, targetSum: int) -> List[List[int]]:
681+
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
682682

683683
result = []
684684
self.traversal(root, targetSum, [], result)
@@ -703,7 +703,7 @@ class Solution:
703703
# self.left = left
704704
# self.right = right
705705
class Solution:
706-
def pathSum(self, root: TreeNode, targetSum: int) -> List[List[int]]:
706+
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
707707
if not root:
708708
return []
709709
stack = [(root, [root.val])]

‎problems/0122.买卖股票的最佳时机II(动态规划).md‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,27 @@ func max(a, b int) int {
251251
}
252252
```
253253

254+
```go
255+
// 动态规划 版本二 滚动数组
256+
func maxProfit(prices []int) int {
257+
dp := [2][2]int{} // 注意这里只开辟了一个2 * 2大小的二维数组
258+
dp[0][0] = -prices[0]
259+
dp[0][1] = 0
260+
for i := 1; i < len(prices); i++ {
261+
dp[i%2][0] = max(dp[(i-1)%2][0], dp[(i - 1) % 2][1] - prices[i])
262+
dp[i%2][1] = max(dp[(i-1)%2][1], dp[(i-1)%2][0] + prices[i])
263+
}
264+
return dp[(len(prices)-1)%2][1]
265+
}
266+
267+
func max(x, y int) int {
268+
if x > y {
269+
return x
270+
}
271+
return y
272+
}
273+
```
274+
254275
### JavaScript:
255276

256277
```javascript

‎problems/0123.买卖股票的最佳时机III.md‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ class Solution:
317317
### Go:
318318

319319
```go
320+
// 版本一
320321
func maxProfit(prices []int) int {
321322
dp := make([][]int, len(prices))
322323
for i := 0; i < len(prices); i++ {
@@ -344,6 +345,58 @@ func max(a, b int) int {
344345
}
345346
```
346347

348+
```go
349+
// 版本二
350+
func maxProfit(prices []int) int {
351+
if len(prices) == 0 {
352+
return 0
353+
}
354+
dp := make([]int, 5)
355+
dp[1] = -prices[0]
356+
dp[3] = -prices[0]
357+
for i := 1; i < len(prices); i++ {
358+
dp[1] = max(dp[1], dp[0] - prices[i])
359+
dp[2] = max(dp[2], dp[1] + prices[i])
360+
dp[3] = max(dp[3], dp[2] - prices[i])
361+
dp[4] = max(dp[4], dp[3] + prices[i])
362+
}
363+
return dp[4]
364+
}
365+
366+
func max(x, y int) int {
367+
if x > y {
368+
return x
369+
}
370+
return y
371+
}
372+
```
373+
374+
```go
375+
// 版本三
376+
func maxProfit(prices []int) int {
377+
if len(prices) == 0 {
378+
return 0
379+
}
380+
dp := make([][5]int, len(prices))
381+
dp[0][1] = -prices[0]
382+
dp[0][3] = -prices[0]
383+
for i := 1; i < len(prices); i++ {
384+
dp[i][1] = max(dp[i-1][1], 0 - prices[i])
385+
dp[i][2] = max(dp[i-1][2], dp[i-1][1] + prices[i])
386+
dp[i][3] = max(dp[i-1][3], dp[i-1][2] - prices[i])
387+
dp[i][4] = max(dp[i-1][4], dp[i-1][3] + prices[i])
388+
}
389+
return dp[len(prices)-1][4]
390+
}
391+
392+
func max(x, y int) int {
393+
if x > y {
394+
return x
395+
}
396+
return y
397+
}
398+
```
399+
347400
### JavaScript:
348401

349402
> 版本一:

0 commit comments

Comments
(0)

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