Skip to content

Commit ac82d12

Browse files
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
2 parents 5ec1375 + feb8539 commit ac82d12

18 files changed

+531
-25
lines changed

problems/0031.下一个排列.md

+7-7
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

+46
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

+50
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/0047.全排列II.md

+34
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,40 @@ func permuteUnique(_ nums: [Int]) -> [[Int]] {
351351
}
352352
```
353353

354+
### Rust
355+
356+
```Rust
357+
impl Solution {
358+
fn backtracking(result: &mut Vec<Vec<i32>>, path: &mut Vec<i32>, nums: &Vec<i32>, used: &mut Vec<bool>) {
359+
let len = nums.len();
360+
if path.len() == len {
361+
result.push(path.clone());
362+
return;
363+
}
364+
for i in 0..len {
365+
if i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false { continue; }
366+
if used[i] == false {
367+
used[i] = true;
368+
path.push(nums[i]);
369+
Self::backtracking(result, path, nums, used);
370+
path.pop();
371+
used[i] = false;
372+
}
373+
}
374+
}
375+
376+
pub fn permute_unique(nums: Vec<i32>) -> Vec<Vec<i32>> {
377+
let mut result: Vec<Vec<i32>> = Vec::new();
378+
let mut path: Vec<i32> = Vec::new();
379+
let mut used = vec![false; nums.len()];
380+
let mut nums= nums;
381+
nums.sort();
382+
Self::backtracking(&mut result, &mut path, &nums, &mut used);
383+
result
384+
}
385+
}
386+
```
387+
354388
### C
355389
```c
356390
//临时数组

problems/0051.N皇后.md

+50
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;

problems/0055.跳跃游戏.md

+20
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,26 @@ var canJump = function(nums) {
154154
};
155155
```
156156

157+
### Rust
158+
159+
```Rust
160+
impl Solution {
161+
fn max(a: usize, b: usize) -> usize {
162+
if a > b { a } else { b }
163+
}
164+
pub fn can_jump(nums: Vec<i32>) -> bool {
165+
let mut cover = 0;
166+
if (nums.len() == 1) { return true; }
167+
let mut i = 0;
168+
while i <= cover {
169+
cover = Self::max(i + nums[i] as usize, cover);
170+
if cover >= nums.len() - 1 { return true; }
171+
i += 1;
172+
}
173+
false
174+
}
175+
}
176+
```
157177

158178
### C
159179
```c

problems/0078.子集.md

+24
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,30 @@ function subsets(nums: number[]): number[][] {
292292
};
293293
```
294294

295+
## Rust
296+
297+
```Rust
298+
impl Solution {
299+
fn backtracking(result: &mut Vec<Vec<i32>>, path: &mut Vec<i32>, nums: &Vec<i32>, start_index: usize) {
300+
result.push(path.clone());
301+
let len = nums.len();
302+
// if start_index >= len { return; }
303+
for i in start_index..len {
304+
path.push(nums[i]);
305+
Self::backtracking(result, path, nums, i + 1);
306+
path.pop();
307+
}
308+
}
309+
310+
pub fn subsets(nums: Vec<i32>) -> Vec<Vec<i32>> {
311+
let mut result: Vec<Vec<i32>> = Vec::new();
312+
let mut path: Vec<i32> = Vec::new();
313+
Self::backtracking(&mut result, &mut path, &nums, 0);
314+
result
315+
}
316+
}
317+
```
318+
295319
## C
296320

297321
```c

problems/0090.子集II.md

+30
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,36 @@ function subsetsWithDup(nums: number[]): number[][] {
367367
};
368368
```
369369

370+
### Rust
371+
372+
```Rust
373+
impl Solution {
374+
fn backtracking(result: &mut Vec<Vec<i32>>, path: &mut Vec<i32>, nums: &Vec<i32>, start_index: usize, used: &mut Vec<bool>) {
375+
result.push(path.clone());
376+
let len = nums.len();
377+
// if start_index >= len { return; }
378+
for i in start_index..len {
379+
if i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false { continue; }
380+
path.push(nums[i]);
381+
used[i] = true;
382+
Self::backtracking(result, path, nums, i + 1, used);
383+
used[i] = false;
384+
path.pop();
385+
}
386+
}
387+
388+
pub fn subsets_with_dup(nums: Vec<i32>) -> Vec<Vec<i32>> {
389+
let mut result: Vec<Vec<i32>> = Vec::new();
390+
let mut path: Vec<i32> = Vec::new();
391+
let mut used = vec![false; nums.len()];
392+
let mut nums = nums;
393+
nums.sort();
394+
Self::backtracking(&mut result, &mut path, &nums, 0, &mut used);
395+
result
396+
}
397+
}
398+
```
399+
370400
### C
371401

372402
```c

problems/0093.复原IP地址.md

+47
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,53 @@ func isNormalIp(s string,startIndex,end int)bool{
536536

537537
```
538538

539+
## Rust
540+
541+
```Rust
542+
impl Solution {
543+
fn is_valid(s: &Vec<char>, start: usize, end: usize) -> bool {
544+
if start > end { return false; }
545+
if s[start] == '0' && start != end { return false; }
546+
let mut num = 0;
547+
for i in start..=end {
548+
if s[i] > '9' || s[i] < '0' { return false; }
549+
if let Some(digit) = s[i].to_digit(10) { num = num * 10 + digit; }
550+
if num > 255 { return false; }
551+
}
552+
true
553+
}
554+
555+
fn backtracking(result: &mut Vec<String>, s: &mut Vec<char>, start_index: usize, mut point_num: usize) {
556+
let len = s.len();
557+
if point_num == 3 {
558+
if Self::is_valid(s, start_index, len - 1) {
559+
result.push(s.iter().collect::<String>());
560+
}
561+
return;
562+
}
563+
for i in start_index..len {
564+
if Self::is_valid(s, start_index, i) {
565+
point_num += 1;
566+
s.insert(i + 1, '.');
567+
Self::backtracking(result, s, i + 2, point_num);
568+
point_num -= 1;
569+
s.remove(i + 1);
570+
} else { break; }
571+
}
572+
}
573+
574+
pub fn restore_ip_addresses(s: String) -> Vec<String> {
575+
let mut result: Vec<String> = Vec::new();
576+
let len = s.len();
577+
if len < 4 || len > 12 { return result; }
578+
let mut s = s.chars().collect::<Vec<char>>();
579+
Self::backtracking(&mut result, &mut s, 0, 0);
580+
result
581+
}
582+
583+
}
584+
```
585+
539586
## C
540587
```c
541588
//记录结果

0 commit comments

Comments
 (0)