Skip to content

Commit 9c9ba93

Browse files
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
2 parents ba5ecfe + 040c241 commit 9c9ba93

Some content is hidden

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

41 files changed

+1445
-190
lines changed

problems/0024.两两交换链表中的节点.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ impl Solution {
409409
// 递归
410410
impl Solution {
411411
pub fn swap_pairs(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
412-
if head == None || head.as_ref().unwrap().next == None {
412+
if head.is_none() || head.as_ref().unwrap().next.is_none() {
413413
return head;
414414
}
415415

problems/0034.在排序数组中查找元素的第一个和最后一个位置.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ class Solution {
240240
while (left - 1 >= 0 && nums[left - 1] == nums[index]) { // 防止数组越界。逻辑短路,两个条件顺序不能换
241241
left--;
242242
}
243-
// 向左滑动,找右边界
243+
// 向右滑动,找右边界
244244
while (right + 1 < nums.length && nums[right + 1] == nums[index]) { // 防止数组越界。
245245
right++;
246246
}

problems/0047.全排列II.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,10 @@ public:
9898
}
9999
};
100100

101+
// 时间复杂度: 最差情况所有元素都是唯一的。复杂度和全排列1都是 O(n! * n) 对于 n 个元素一共有 n! 中排列方案。而对于每一个答案,我们需要 O(n) 去复制最终放到 result 数组
102+
// 空间复杂度: O(n) 回溯树的深度取决于我们有多少个元素
101103
```
102-
* 时间复杂度: O(n)
104+
* 时间复杂度: O(n! * n)
103105
* 空间复杂度: O(n)
104106
105107
## 拓展

problems/0063.不同路径II.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,39 @@ class Solution:
397397

398398
```
399399

400+
动态规划(版本五)
400401

402+
```python
403+
class Solution:
404+
def uniquePathsWithObstacles(self, obstacleGrid):
405+
if obstacleGrid[0][0] == 1:
406+
return 0
407+
408+
m, n = len(obstacleGrid), len(obstacleGrid[0])
409+
410+
dp = [0] * n # 创建一个一维列表用于存储路径数
411+
412+
# 初始化第一行的路径数
413+
for j in range(n):
414+
if obstacleGrid[0][j] == 1:
415+
break
416+
dp[j] = 1
417+
418+
# 计算其他行的路径数
419+
for i in range(1, m):
420+
if obstacleGrid[i][0] == 1:
421+
dp[0] = 0
422+
for j in range(1, n):
423+
if obstacleGrid[i][j] == 1:
424+
dp[j] = 0
425+
continue
426+
427+
dp[j] += dp[j - 1]
428+
429+
return dp[-1] # 返回最后一个元素,即终点的路径数
430+
431+
432+
```
401433
### Go
402434

403435
```go

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,13 @@ Java:
133133
class Solution {
134134
public int climbStairs(int n) {
135135
int[] dp = new int[n + 1];
136-
int m = 2;
136+
int m = 2; //有兩個物品:itme1重量爲一,item2重量爲二
137137
dp[0] = 1;
138138
139139
for (int i = 1; i <= n; i++) { // 遍历背包
140140
for (int j = 1; j <= m; j++) { //遍历物品
141-
if (i >= j) dp[i] += dp[i - j];
141+
if (i >= j) //當前的背包容量 大於 物品重量的時候,我們才需要記錄當前的這個裝得方法(方法數+)
142+
dp[i] += dp[i - j];
142143
}
143144
}
144145

problems/0072.编辑距离.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ exection -> execution (插入 'u')
4040
* 0 <= word1.length, word2.length <= 500
4141
* word1 和 word2 由小写英文字母组成
4242

43+
# 算法公开课
44+
**《代码随想录》算法视频公开课:[动态规划终极绝杀! LeetCode:72.编辑距离](https://www.bilibili.com/video/BV1we4y157wB/),相信结合视频再看本篇题解,更有助于大家对本题的理解**
4345

4446
## 思路
4547

problems/0121.买卖股票的最佳时机.md

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,27 @@ class Solution {
243243
}
244244
}
245245
```
246+
> 动态规划:版本二(使用二維數組(和卡哥思路一致),下面還有使用一維滾動數組的更優化版本)
246247

247-
> 动态规划:版本二
248+
```Java
249+
class Solution {
250+
public int maxProfit(int[] prices) {
251+
int len = prices.length;
252+
int dp[][] = new int[2][2];
253+
254+
dp[0][0] = - prices[0];
255+
dp[0][1] = 0;
256+
257+
for (int i = 1; i < len; i++){
258+
dp[i % 2][0] = Math.max(dp[(i - 1) % 2][0], - prices[i]);
259+
dp[i % 2][1] = Math.max(dp[(i - 1) % 2][1], prices[i] + dp[(i - 1) % 2][0]);
260+
}
261+
return dp[(len - 1) % 2][1];
262+
}
263+
}
264+
```
265+
266+
> 动态规划:版本二(使用一維數組)
248267

249268
``` java
250269
class Solution {
@@ -271,6 +290,10 @@ class Solution {
271290
}
272291
}
273292
```
293+
```Java
294+
295+
```
296+
274297

275298
Python:
276299

@@ -510,7 +533,37 @@ public class Solution
510533
}
511534
```
512535

536+
Rust:
537+
538+
> 贪心
539+
540+
```rust
541+
impl Solution {
542+
pub fn max_profit(prices: Vec<i32>) -> i32 {
543+
let (mut low, mut res) = (i32::MAX, 0);
544+
for p in prices {
545+
low = p.min(low);
546+
res = res.max(p - low);
547+
}
548+
res
549+
}
550+
}
551+
```
513552

553+
> 动态规划
554+
555+
```rust
556+
impl Solution {
557+
pub fn max_profit(prices: Vec<i32>) -> i32 {
558+
let mut dp = vec![-prices[0], 0];
559+
for p in prices {
560+
dp[0] = dp[0].max(-p);
561+
dp[1] = dp[1].max(dp[0] + p);
562+
}
563+
dp[1]
564+
}
565+
}
566+
```
514567

515568

516569
<p align="center">

problems/0122.买卖股票的最佳时机II.md

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -322,13 +322,10 @@ function maxProfit(prices: number[]): number {
322322

323323
```Rust
324324
impl Solution {
325-
fn max(a: i32, b: i32) -> i32 {
326-
if a > b { a } else { b }
327-
}
328325
pub fn max_profit(prices: Vec<i32>) -> i32 {
329326
let mut result = 0;
330327
for i in 1..prices.len() {
331-
result += Self::max(prices[i] - prices[i - 1], 0);
328+
result += (prices[i] - prices[i - 1]).max(0);
332329
}
333330
result
334331
}
@@ -339,18 +336,14 @@ impl Solution {
339336

340337
```Rust
341338
impl Solution {
342-
fn max(a: i32, b: i32) -> i32 {
343-
if a > b { a } else { b }
344-
}
345339
pub fn max_profit(prices: Vec<i32>) -> i32 {
346-
let n = prices.len();
347-
let mut dp = vec![vec![0; 2]; n];
348-
dp[0][0] -= prices[0];
349-
for i in 1..n {
350-
dp[i][0] = Self::max(dp[i - 1][0], dp[i - 1][1] - prices[i]);
351-
dp[i][1] = Self::max(dp[i - 1][1], dp[i - 1][0] + prices[i]);
340+
let mut dp = vec![vec![0; 2]; prices.len()];
341+
dp[0][0] = -prices[0];
342+
for i in 1..prices.len() {
343+
dp[i][0] = dp[i - 1][0].max(dp[i - 1][1] - prices[i]);
344+
dp[i][1] = dp[i - 1][1].max(dp[i - 1][0] + prices[i]);
352345
}
353-
Self::max(dp[n - 1][0], dp[n - 1][1])
346+
dp[prices.len() - 1][1]
354347
}
355348
}
356349
```

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,24 @@ class Solution
154154
}
155155
}
156156
```
157+
```java
158+
//DP using 2*2 Array (下方還有使用一維滾動數組的更優化版本)
159+
class Solution {
160+
public int maxProfit(int[] prices) {
161+
int dp[][] = new int [2][2];
162+
//dp[i][0]: holding the stock
163+
//dp[i][1]: not holding the stock
164+
dp[0][0] = - prices[0];
165+
dp[0][1] = 0;
157166

167+
for(int i = 1; i < prices.length; i++){
168+
dp[i % 2][0] = Math.max(dp[(i - 1) % 2][0], dp[(i - 1) % 2][1] - prices[i]);
169+
dp[i % 2][1] = Math.max(dp[(i - 1) % 2][1], dp[(i - 1) % 2][0] + prices[i]);
170+
}
171+
return dp[(prices.length - 1) % 2][1];
172+
}
173+
}
174+
```
158175
```java
159176
// 优化空间
160177
class Solution {
@@ -346,7 +363,52 @@ public class Solution
346363
}
347364
```
348365

366+
Rust:
367+
368+
> 贪心
349369
370+
```rust
371+
impl Solution {
372+
pub fn max_profit(prices: Vec<i32>) -> i32 {
373+
let mut result = 0;
374+
for i in 1..prices.len() {
375+
result += (prices[i] - prices[i - 1]).max(0);
376+
}
377+
result
378+
}
379+
}
380+
```
381+
382+
>动态规划
383+
384+
```rust
385+
impl Solution {
386+
pub fn max_profit(prices: Vec<i32>) -> i32 {
387+
let mut dp = vec![vec![0; 2]; prices.len()];
388+
dp[0][0] = -prices[0];
389+
for i in 1..prices.len() {
390+
dp[i][0] = dp[i - 1][0].max(dp[i - 1][1] - prices[i]);
391+
dp[i][1] = dp[i - 1][1].max(dp[i - 1][0] + prices[i]);
392+
}
393+
dp[prices.len() - 1][1]
394+
}
395+
}
396+
```
397+
398+
> 优化
399+
400+
```rust
401+
impl Solution {
402+
pub fn max_profit(prices: Vec<i32>) -> i32 {
403+
let mut dp = vec![-prices[0], 0];
404+
for p in prices {
405+
dp[0] = dp[0].max(dp[1] - p);
406+
dp[1] = dp[1].max(dp[0] + p);
407+
}
408+
dp[1]
409+
}
410+
}
411+
```
350412

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

problems/0130.被围绕的区域.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,54 @@ class Solution {
188188
}
189189
}
190190
```
191+
```Java
192+
//BFS(使用helper function)
193+
class Solution {
194+
int[][] dir ={{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
195+
public void solve(char[][] board) {
196+
for(int i = 0; i < board.length; i++){
197+
if(board[i][0] == 'O') bfs(board, i, 0);
198+
if(board[i][board[0].length - 1] == 'O') bfs(board, i, board[0].length - 1);
199+
}
200+
201+
for(int j = 1 ; j < board[0].length - 1; j++){
202+
if(board[0][j] == 'O') bfs(board, 0, j);
203+
if(board[board.length - 1][j] == 'O') bfs(board, board.length - 1, j);
204+
}
205+
206+
for(int i = 0; i < board.length; i++){
207+
for(int j = 0; j < board[0].length; j++){
208+
if(board[i][j] == 'O') board[i][j] = 'X';
209+
if(board[i][j] == 'A') board[i][j] = 'O';
210+
}
211+
}
212+
}
213+
private void bfs(char[][] board, int x, int y){
214+
Queue<Integer> que = new LinkedList<>();
215+
board[x][y] = 'A';
216+
que.offer(x);
217+
que.offer(y);
218+
219+
while(!que.isEmpty()){
220+
int currX = que.poll();
221+
int currY = que.poll();
222+
223+
for(int i = 0; i < 4; i++){
224+
int nextX = currX + dir[i][0];
225+
int nextY = currY + dir[i][1];
226+
227+
if(nextX < 0 || nextY < 0 || nextX >= board.length || nextY >= board[0].length)
228+
continue;
229+
if(board[nextX][nextY] == 'X'|| board[nextX][nextY] == 'A')
230+
continue;
231+
bfs(board, nextX, nextY);
232+
}
233+
}
234+
}
235+
}
236+
237+
```
238+
191239
```Java
192240
// 深度优先遍历
193241
// 使用 visited 数组进行标记
@@ -296,6 +344,47 @@ class Solution {
296344
}
297345
}
298346
```
347+
```java
348+
//DFS(有終止條件)
349+
class Solution {
350+
int[][] dir ={{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
351+
public void solve(char[][] board) {
352+
353+
for(int i = 0; i < board.length; i++){
354+
if(board[i][0] == 'O') dfs(board, i, 0);
355+
if(board[i][board[0].length - 1] == 'O') dfs(board, i, board[0].length - 1);
356+
}
357+
358+
for(int j = 1 ; j < board[0].length - 1; j++){
359+
if(board[0][j] == 'O') dfs(board, 0, j);
360+
if(board[board.length - 1][j] == 'O') dfs(board, board.length - 1, j);
361+
}
362+
363+
for(int i = 0; i < board.length; i++){
364+
for(int j = 0; j < board[0].length; j++){
365+
if(board[i][j] == 'O') board[i][j] = 'X';
366+
if(board[i][j] == 'A') board[i][j] = 'O';
367+
}
368+
}
369+
}
370+
371+
private void dfs(char[][] board, int x, int y){
372+
if(board[x][y] == 'X'|| board[x][y] == 'A')
373+
return;
374+
board[x][y] = 'A';
375+
for(int i = 0; i < 4; i++){
376+
int nextX = x + dir[i][0];
377+
int nextY = y + dir[i][1];
378+
379+
if(nextX < 0 || nextY < 0 || nextX >= board.length || nextY >= board[0].length)
380+
continue;
381+
// if(board[nextX][nextY] == 'X'|| board[nextX][nextY] == 'A')
382+
// continue;
383+
dfs(board, nextX, nextY);
384+
}
385+
}
386+
}
387+
```
299388

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

0 commit comments

Comments
 (0)