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 26866fa

Browse files
Merge branch 'youngyangyang04:master' into master
2 parents 5868d5e + 5374ab5 commit 26866fa

File tree

57 files changed

+697
-245
lines changed

Some content is hidden

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

57 files changed

+697
-245
lines changed

‎README.md‎

Lines changed: 63 additions & 81 deletions
Large diffs are not rendered by default.

‎problems/0001.两数之和.md‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,15 @@ php
263263
```php
264264
function twoSum(array $nums, int $target): array
265265
{
266-
for ($i = 0; $i < count($nums);$i++) {
267-
// 计算剩下的数
268-
$residue = $target - $nums[$i];
269-
// 匹配的index,有则返回index, 无则返回false
270-
$match_index = array_search($residue, $nums);
271-
if ($match_index !== false && $match_index != $i) {
272-
return array($i, $match_index);
266+
$map = [];
267+
foreach($nums as $i => $num) {
268+
if (isset($map[$target - $num])) {
269+
return [
270+
$i,
271+
$map[$target - $num]
272+
];
273+
} else {
274+
$map[$num] = $i;
273275
}
274276
}
275277
return [];

‎problems/0018.四数之和.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public:
7474
sort(nums.begin(), nums.end());
7575
for (int k = 0; k < nums.size(); k++) {
7676
// 剪枝处理
77-
if (nums[k] > target && nums[k] >= 0 && target >= 0) {
77+
if (nums[k] > target && nums[k] >= 0) {
7878
break; // 这里使用break,统一通过最后的return返回
7979
}
8080
// 对nums[k]去重
@@ -83,7 +83,7 @@ public:
8383
}
8484
for (int i = k + 1; i < nums.size(); i++) {
8585
// 2级剪枝处理
86-
if (nums[k] + nums[i] > target && nums[k] + nums[i] >= 0 && target >= 0) {
86+
if (nums[k] + nums[i] > target && nums[k] + nums[i] >= 0) {
8787
break;
8888
}
8989

‎problems/0019.删除链表的倒数第N个节点.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333

3434
## 思路
3535

36+
《代码随想录》算法公开课:[链表遍历学清楚! | LeetCode:19.删除链表倒数第N个节点](https://www.bilibili.com/video/BV1vW4y1U7Gf),相信结合视频在看本篇题解,更有助于大家对链表的理解。
37+
38+
3639
双指针的经典应用,如果要删除倒数第n个节点,让fast移动n步,然后让fast和slow同时移动,直到fast指向链表末尾。删掉slow所指向的节点就可以了。
3740

3841
思路是这样的,但要注意一些细节。

‎problems/0020.有效的括号.md‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141

4242
# 思路
4343

44+
《代码随想录》算法视频公开课:[栈的拿手好戏!| LeetCode:20. 有效的括号](https://www.bilibili.com/video/BV1AF411w78g),相信结合视频在看本篇题解,更有助于大家对链表的理解。
45+
46+
4447
## 题外话
4548

4649
**括号匹配是使用栈解决的经典问题。**
@@ -79,8 +82,10 @@ cd a/b/c/../../
7982

8083
1. 第一种情况,字符串里左方向的括号多余了 ,所以不匹配。
8184
![括号匹配1](https://img-blog.csdnimg.cn/2020080915505387.png)
85+
8286
2. 第二种情况,括号没有多余,但是 括号的类型没有匹配上。
8387
![括号匹配2](https://img-blog.csdnimg.cn/20200809155107397.png)
88+
8489
3. 第三种情况,字符串里右方向的括号多余了,所以不匹配。
8590
![括号匹配3](https://img-blog.csdnimg.cn/20200809155115779.png)
8691

@@ -110,7 +115,8 @@ cd a/b/c/../../
110115
class Solution {
111116
public:
112117
bool isValid(string s) {
113-
stack<int> st;
118+
if (s.size() % 2 != 0) return false; // 如果s的长度为奇数,一定不符合要求
119+
stack<char> st;
114120
for (int i = 0; i < s.size(); i++) {
115121
if (s[i] == '(') st.push(')');
116122
else if (s[i] == '{') st.push('}');
@@ -124,6 +130,7 @@ public:
124130
return st.empty();
125131
}
126132
};
133+
127134
```
128135
技巧性的东西没有固定的学习方法,还是要多看多练,自己灵活运用了。
129136

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818

1919
## 思路
2020

21-
针对本题重点难点,我录制了B站讲解视频,[帮你把链表细节学清楚! | LeetCode:24. 两两交换链表中的节点](https://www.bilibili.com/video/BV1YT411g7br),相信结合视频在看本篇题解,更有助于大家对链表的理解。
21+
《代码随想录》算法公开课:[帮你把链表细节学清楚! | LeetCode:24. 两两交换链表中的节点](https://www.bilibili.com/video/BV1YT411g7br),相信结合视频在看本篇题解,更有助于大家对链表的理解。
22+
2223

2324
这道题目正常模拟就可以了。
2425

‎problems/0031.下一个排列.md‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,10 @@ class Solution:
178178
>另一种思路
179179
```python
180180
class Solution:
181-
'''
182-
抛砖引玉:因题目要求"必须原地修改,只允许使用额外常数空间",python内置sorted函数以及数组切片+sort()无法使用。
183-
故选择另一种算法暂且提供一种python思路
184-
'''
181+
'''
182+
抛砖引玉:因题目要求"必须原地修改,只允许使用额外常数空间",python内置sorted函数以及数组切片+sort()无法使用。
183+
故选择另一种算法暂且提供一种python思路
184+
'''
185185
def nextPermutation(self, nums: List[int]) -> None:
186186
"""
187187
Do not return anything, modify nums in-place instead.
@@ -195,9 +195,9 @@ class Solution:
195195
break
196196
self.reverse(nums, i, length-1)
197197
break
198-
else:
199-
# 若正常结束循环,则对原数组直接翻转
200-
self.reverse(nums, 0, length-1)
198+
if n == 1:
199+
# 若正常结束循环,则对原数组直接翻转
200+
self.reverse(nums, 0, length-1)
201201

202202
def reverse(self, nums: List[int], low: int, high: int) -> None:
203203
while low < high:

‎problems/0037.解数独.md‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,52 @@ function solveSudoku(board: string[][]): void {
488488
};
489489
```
490490

491+
### Rust
492+
493+
```Rust
494+
impl Solution {
495+
fn is_valid(row: usize, col: usize, val: char, board: &mut Vec<Vec<char>>) -> bool{
496+
for i in 0..9 {
497+
if board[row][i] == val { return false; }
498+
}
499+
for j in 0..9 {
500+
if board[j][col] == val {
501+
return false;
502+
}
503+
}
504+
let start_row = (row / 3) * 3;
505+
let start_col = (col / 3) * 3;
506+
for i in start_row..(start_row + 3) {
507+
for j in start_col..(start_col + 3) {
508+
if board[i][j] == val { return false; }
509+
}
510+
}
511+
return true;
512+
}
513+
514+
fn backtracking(board: &mut Vec<Vec<char>>) -> bool{
515+
for i in 0..board.len() {
516+
for j in 0..board[0].len() {
517+
if board[i][j] != '.' { continue; }
518+
for k in '1'..='9' {
519+
if Self::is_valid(i, j, k, board) {
520+
board[i][j] = k;
521+
if Self::backtracking(board) { return true; }
522+
board[i][j] = '.';
523+
}
524+
}
525+
return false;
526+
}
527+
}
528+
return true;
529+
}
530+
531+
pub fn solve_sudoku(board: &mut Vec<Vec<char>>) {
532+
Self::backtracking(board);
533+
}
534+
}
535+
```
536+
491537
### C
492538

493539
```C

‎problems/0045.跳跃游戏II.md‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,56 @@ object Solution {
305305
}
306306
```
307307

308+
### Rust
309+
310+
```Rust
311+
//版本一
312+
impl Solution {
313+
fn max(a: i32, b:i32) -> i32 {
314+
if a > b { a } else { b }
315+
}
316+
pub fn jump(nums: Vec<i32>) -> i32 {
317+
if nums.len() == 0 { return 0; }
318+
let mut cur_distance: i32 = 0;
319+
let mut ans: i32 = 0;
320+
let mut next_distance: i32 = 0;
321+
for i in 0..nums.len() {
322+
next_distance = Self::max(nums[i] + i as i32, next_distance);
323+
if i as i32 == cur_distance {
324+
if cur_distance != (nums.len() - 1) as i32 {
325+
ans += 1;
326+
cur_distance = next_distance;
327+
if next_distance == (nums.len() - 1) as i32 { break; }
328+
}
329+
else { break; }
330+
}
331+
}
332+
ans
333+
}
334+
}
335+
```
336+
337+
```Rust
338+
//版本二
339+
impl Solution {
340+
fn max(a: i32, b:i32) -> i32 {
341+
if a > b { a } else { b }
342+
}
343+
pub fn jump(nums: Vec<i32>) -> i32 {
344+
let mut cur_distance: i32 = 0;
345+
let mut ans: i32 = 0;
346+
let mut next_distance: i32 = 0;
347+
for i in 0..nums.len() - 1 {
348+
next_distance = Self::max(nums[i] + i as i32, next_distance);
349+
if i as i32 == cur_distance {
350+
cur_distance = next_distance;
351+
ans += 1;
352+
}
353+
}
354+
ans
355+
}
356+
}
357+
```
308358

309359

310360
-----------------------

‎problems/0051.N皇后.md‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,56 @@ func solveNQueens(_ n: Int) -> [[String]] {
558558
}
559559
```
560560

561+
### Rust
562+
563+
```Rust
564+
impl Solution {
565+
fn is_valid(row: usize, col: usize, chessboard: &mut Vec<Vec<char>>, n: usize) -> bool {
566+
let mut i = 0 as usize;
567+
while i < row {
568+
if chessboard[i][col] == 'Q' { return false; }
569+
i += 1;
570+
}
571+
let (mut i, mut j) = (row as i32 - 1, col as i32 - 1);
572+
while i >= 0 && j >= 0 {
573+
if chessboard[i as usize][j as usize] == 'Q' { return false; }
574+
i -= 1;
575+
j -= 1;
576+
}
577+
let (mut i, mut j) = (row as i32 - 1, col as i32 + 1);
578+
while i >= 0 && j < n as i32 {
579+
if chessboard[i as usize][j as usize] == 'Q' { return false; }
580+
i -= 1;
581+
j += 1;
582+
}
583+
return true;
584+
}
585+
fn backtracking(result: &mut Vec<Vec<String>>, n: usize, row: usize, chessboard: &mut Vec<Vec<char>>) {
586+
if row == n {
587+
let mut chessboard_clone: Vec<String> = Vec::new();
588+
for i in chessboard {
589+
chessboard_clone.push(i.iter().collect::<String>());
590+
}
591+
result.push(chessboard_clone);
592+
return;
593+
}
594+
for col in 0..n {
595+
if Self::is_valid(row, col, chessboard, n) {
596+
chessboard[row][col] = 'Q';
597+
Self::backtracking(result, n, row + 1, chessboard);
598+
chessboard[row][col] = '.';
599+
}
600+
}
601+
}
602+
pub fn solve_n_queens(n: i32) -> Vec<Vec<String>> {
603+
let mut result: Vec<Vec<String>> = Vec::new();
604+
let mut chessboard: Vec<Vec<char>> = vec![vec!['.'; n as usize]; n as usize];
605+
Self::backtracking(&mut result, n as usize, 0, &mut chessboard);
606+
result
607+
}
608+
}
609+
```
610+
561611
### C
562612
```c
563613
char ***ans;

0 commit comments

Comments
(0)

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