Skip to content

Commit c00b3c6

Browse files
committed
Merge branch 'master' of github.com:flames519/leetcode-master
2 parents 2b5b520 + 0135feb commit c00b3c6

File tree

85 files changed

+3417
-335
lines changed

Some content is hidden

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

85 files changed

+3417
-335
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
<a href="https://space.bilibili.com/525438321"><img src="https://img.shields.io/badge/B站-代码随想录-orange" alt=""></a>
1818
<a href="https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ"><img src="https://img.shields.io/badge/知识星球-代码随想录-blue" alt=""></a>
1919
</p>
20+
<p align="center">
21+
<a href="https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ" target="_blank">
22+
<img src="./pics/知识星球.png" width="600"/>
23+
</a>
24+
2025

2126
# LeetCode 刷题攻略
2227

@@ -120,6 +125,10 @@
120125
4. [马上秋招了,慌得很!](https://mp.weixin.qq.com/s/7q7W8Cb2-a5U5atZdOnOFA)
121126
5. [Carl看了上百份简历,总结了这些!](https://mp.weixin.qq.com/s/sJa87MZD28piCOVMFkIbwQ)
122127
6. [面试中遇到了发散性问题.....](https://mp.weixin.qq.com/s/SSonDxi2pjkSVwHNzZswng)
128+
7. [英语到底重不重要!](https://mp.weixin.qq.com/s/1PRZiyF_-TVA-ipwDNjdKw)
129+
8. [计算机专业要不要读研!](https://mp.weixin.qq.com/s/c9v1L3IjqiXtkNH7sOMAdg)
130+
9. [秋招和提前批都越来越提前了....](https://mp.weixin.qq.com/s/SNFiRDx8CKyjhTPlys6ywQ)
131+
123132

124133
## 数组
125134

@@ -379,9 +388,12 @@
379388
54. [动态规划:最长回文子序列](./problems/0516.最长回文子序列.md)
380389
55. [动态规划总结篇](./problems/动态规划总结篇.md)
381390

382-
383391
(持续更新中....)
384392

393+
## 单调栈
394+
395+
1. [每日温度](./problems/0739.每日温度.md)
396+
385397
## 图论
386398

387399
## 十大排序

problems/0001.两数之和.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public int[] twoSum(int[] nums, int target) {
107107

108108
Python:
109109

110-
```python3
110+
```python
111111
class Solution:
112112
def twoSum(self, nums: List[int], target: int) -> List[int]:
113113
hashmap={}

problems/0037.解数独.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,59 @@ class Solution:
321321
backtrack(board)
322322
```
323323

324+
Python3:
325+
326+
```python3
327+
class Solution:
328+
def __init__(self) -> None:
329+
self.board = []
330+
331+
def isValid(self, row: int, col: int, target: int) -> bool:
332+
for idx in range(len(self.board)):
333+
# 同列是否重复
334+
if self.board[idx][col] == str(target):
335+
return False
336+
# 同行是否重复
337+
if self.board[row][idx] == str(target):
338+
return False
339+
# 9宫格里是否重复
340+
box_row, box_col = (row // 3) * 3 + idx // 3, (col // 3) * 3 + idx % 3
341+
if self.board[box_row][box_col] == str(target):
342+
return False
343+
return True
344+
345+
def getPlace(self) -> List[int]:
346+
for row in range(len(self.board)):
347+
for col in range(len(self.board)):
348+
if self.board[row][col] == ".":
349+
return [row, col]
350+
return [-1, -1]
351+
352+
def isSolved(self) -> bool:
353+
row, col = self.getPlace() # 找个空位置
354+
355+
if row == -1 and col == -1: # 没有空位置,棋盘被填满的
356+
return True
357+
358+
for i in range(1, 10):
359+
if self.isValid(row, col, i): # 检查这个空位置放i,是否合适
360+
self.board[row][col] = str(i) # 放i
361+
if self.isSolved(): # 合适,立刻返回, 填下一个空位置。
362+
return True
363+
self.board[row][col] = "." # 不合适,回溯
364+
365+
return False # 空位置没法解决
366+
367+
def solveSudoku(self, board: List[List[str]]) -> None:
368+
"""
369+
Do not return anything, modify board in-place instead.
370+
"""
371+
if board is None or len(board) == 0:
372+
return
373+
self.board = board
374+
self.isSolved()
375+
```
376+
324377
Go:
325378

326379
Javascript:

problems/0051.N皇后.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -353,14 +353,6 @@ class Solution {
353353
}
354354
```
355355

356-
## 其他语言版本
357-
358-
359-
Java:
360-
361-
362-
Python:
363-
364356

365357
Go:
366358
```Go

problems/0053.最大子序和(动态规划).md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,47 @@ public:
9595
9696
9797
Java:
98+
```java
99+
/**
100+
* 1.dp[i]代表当前下标对应的最大值
101+
* 2.递推公式 dp[i] = max (dp[i-1]+nums[i],nums[i]) res = max(res,dp[i])
102+
* 3.初始化 都为 0
103+
* 4.遍历方向,从前往后
104+
* 5.举例推导结果。。。
105+
*
106+
* @param nums
107+
* @return
108+
*/
109+
public static int maxSubArray(int[] nums) {
110+
if (nums.length == 0) {
111+
return 0;
112+
}
98113
114+
int res = nums[0];
115+
int[] dp = new int[nums.length];
116+
dp[0] = nums[0];
117+
for (int i = 1; i < nums.length; i++) {
118+
dp[i] = Math.max(dp[i - 1] + nums[i], nums[i]);
119+
res = res > dp[i] ? res : dp[i];
120+
}
121+
return res;
122+
}
123+
```
99124

100125
Python:
101-
126+
```python
127+
class Solution:
128+
def maxSubArray(self, nums: List[int]) -> int:
129+
if len(nums) == 0:
130+
return 0
131+
dp = [0] * len(nums)
132+
dp[0] = nums[0]
133+
result = dp[0]
134+
for i in range(1, len(nums)):
135+
dp[i] = max(dp[i-1] + nums[i], nums[i]) #状态转移公式
136+
result = max(result, dp[i]) #result 保存dp[i]的最大值
137+
return result
138+
```
102139

103140
Go:
104141

problems/0062.不同路径.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,27 @@ func uniquePaths(m int, n int) int {
308308
}
309309
```
310310

311+
Javascript:
312+
```Javascript
313+
var uniquePaths = function(m, n) {
314+
const dp = Array(m).fill().map(item => Array(n))
315+
316+
for (let i = 0; i < m; ++i) {
317+
dp[i][0] = 1
318+
}
319+
320+
for (let i = 0; i < n; ++i) {
321+
dp[0][i] = 1
322+
}
323+
324+
for (let i = 1; i < m; ++i) {
325+
for (let j = 1; j < n; ++j) {
326+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
327+
}
328+
}
329+
return dp[m - 1][n - 1]
330+
};
331+
```
311332

312333

313334
-----------------------

problems/0063.不同路径II.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,30 @@ func uniquePathsWithObstacles(obstacleGrid [][]int) int {
279279

280280
```
281281

282+
Javascript
283+
``` Javascript
284+
var uniquePathsWithObstacles = function(obstacleGrid) {
285+
const m = obstacleGrid.length
286+
const n = obstacleGrid[0].length
287+
const dp = Array(m).fill().map(item => Array(n).fill(0))
288+
289+
for (let i = 0; i < m && obstacleGrid[i][0] === 0; ++i) {
290+
dp[i][0] = 1
291+
}
292+
293+
for (let i = 0; i < n && obstacleGrid[0][i] === 0; ++i) {
294+
dp[0][i] = 1
295+
}
296+
297+
for (let i = 1; i < m; ++i) {
298+
for (let j = 1; j < n; ++j) {
299+
dp[i][j] = obstacleGrid[i][j] === 1 ? 0 : dp[i - 1][j] + dp[i][j - 1]
300+
}
301+
}
302+
303+
return dp[m - 1][n - 1]
304+
};
305+
```
282306

283307

284308
-----------------------

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,43 @@ class Solution {
148148
Python:
149149

150150

151-
Go:
151+
```python3
152+
class Solution:
153+
def climbStairs(self, n: int) -> int:
154+
dp = [0]*(n + 1)
155+
dp[0] = 1
156+
m = 2
157+
# 遍历背包
158+
for j in range(n + 1):
159+
# 遍历物品
160+
for step in range(1, m + 1):
161+
if j >= step:
162+
dp[j] += dp[j - step]
163+
return dp[n]
164+
```
165+
152166

167+
Go:
168+
```go
169+
func climbStairs(n int) int {
170+
//定义
171+
dp := make([]int, n+1)
172+
//初始化
173+
dp[0] = 1
174+
// 本题物品只有两个1,2
175+
m := 2
176+
// 遍历顺序
177+
for j := 1; j <= n; j++ { //先遍历背包
178+
for i := 1; i <= m; i++ { //再遍历物品
179+
if j >= i {
180+
dp[j] += dp[j-i]
181+
}
182+
//fmt.Println(dp)
183+
}
184+
}
185+
return dp[n]
186+
}
187+
```
153188

154189

155190

0 commit comments

Comments
 (0)