Skip to content

Commit 710a16e

Browse files
committed
add 3;17;32;62;63;64
1 parent cc0e002 commit 710a16e

7 files changed

+237
-0
lines changed

10.regular-expression-matching.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* @lc app=leetcode id=10 lang=rust
3+
*
4+
* [10] Regular Expression Matching
5+
*/
6+
7+
// @lc code=start
8+
impl Solution {
9+
pub fn is_match(s: String, p: String) -> bool {
10+
11+
}
12+
}
13+
// @lc code=end
14+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* @lc app=leetcode id=17 lang=rust
3+
*
4+
* [17] Letter Combinations of a Phone Number
5+
*/
6+
7+
// @lc code=start
8+
use std::collections::HashMap;
9+
10+
impl Solution {
11+
pub fn letter_combinations(digits: String) -> Vec<String> {
12+
if digits.len() == 0 {
13+
return vec![];
14+
}
15+
let mut ans = vec!["".to_string()];
16+
let map: HashMap<char, Vec<char>> = [
17+
('2', vec!['a', 'b', 'c']),
18+
('3', vec!['d', 'e', 'f']),
19+
('4', vec!['g', 'h', 'i']),
20+
('5', vec!['j', 'k', 'l']),
21+
('6', vec!['m', 'n', 'o']),
22+
('7', vec!['p', 'q', 'r', 's']),
23+
('8', vec!['t', 'u', 'v']),
24+
('9', vec!['w', 'x', 'y', 'z'])
25+
].iter().cloned().collect();
26+
27+
for d in digits.chars() {
28+
let mut tmp = vec![];
29+
if let Some(v) = map.get(&d) {
30+
for s in &ans {
31+
for ch in v {
32+
tmp.push(s.clone() + &ch.to_string());
33+
}
34+
}
35+
}
36+
ans = tmp;
37+
}
38+
39+
ans
40+
}
41+
}
42+
// @lc code=end
43+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* @lc app=leetcode id=3 lang=rust
3+
*
4+
* [3] Longest Substring Without Repeating Characters
5+
*/
6+
7+
// @lc code=start
8+
use std::collections::HashMap;
9+
use std::cmp;
10+
11+
impl Solution {
12+
pub fn length_of_longest_substring(s: String) -> i32 {
13+
let mut map = HashMap::new();
14+
let chars: Vec<char> = s.chars().collect();
15+
let mut start = -1;
16+
let mut ans = 0;
17+
18+
for i in 0..s.len() {
19+
if let Some(v) = map.get_mut(&chars[i]) {
20+
start = cmp::max(start, *v);
21+
}
22+
ans = cmp::max(ans, i as i32 - start);
23+
map.insert(&chars[i], i as i32);
24+
}
25+
26+
ans
27+
}
28+
}
29+
// @lc code=end
30+

32.longest-valid-parentheses.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* @lc app=leetcode id=32 lang=rust
3+
*
4+
* [32] Longest Valid Parentheses
5+
*/
6+
7+
// @lc code=start
8+
impl Solution {
9+
pub fn longest_valid_parentheses(s: String) -> i32 {
10+
11+
}
12+
}
13+
// @lc code=end
14+

62.unique-paths.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* @lc app=leetcode id=62 lang=rust
3+
*
4+
* [62] Unique Paths
5+
*/
6+
7+
// @lc code=start
8+
impl Solution {
9+
pub fn unique_paths(m: i32, n: i32) -> i32 {
10+
if m <= 0 || n <= 0 {
11+
return 0;
12+
}
13+
14+
let mut dp = vec![vec![1; n as usize]; m as usize];
15+
16+
for i in (0..m as usize).rev() {
17+
for j in (0..n as usize).rev() {
18+
if i == (m - 1) as usize {
19+
if j < (n - 1) as usize {
20+
dp[i][j] = dp[i][j+1];
21+
}
22+
} else {
23+
if j == (n - 1) as usize {
24+
dp[i][j] = dp[i+1][j];
25+
} else {
26+
dp[i][j] = dp[i+1][j] + dp[i][j+1];
27+
}
28+
}
29+
}
30+
}
31+
32+
dp[0][0]
33+
}
34+
}
35+
// @lc code=end
36+

63.unique-paths-ii.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* @lc app=leetcode id=63 lang=rust
3+
*
4+
* [63] Unique Paths II
5+
*/
6+
7+
// @lc code=start
8+
impl Solution {
9+
pub fn unique_paths_with_obstacles(obstacle_grid: Vec<Vec<i32>>) -> i32 {
10+
let R = obstacle_grid.len();
11+
if R == 0 { return 0; }
12+
13+
let C = obstacle_grid[0].len();
14+
if C == 0 { return 0; }
15+
16+
17+
let mut dp = vec![vec![1; C as usize]; R as usize];
18+
19+
for i in (0..R as usize).rev() {
20+
for j in (0..C as usize).rev() {
21+
if i == (R - 1) as usize {
22+
if j < (C - 1) as usize {
23+
if obstacle_grid[i][j] == 1 {
24+
dp[i][j] = 0;
25+
} else {
26+
dp[i][j] = dp[i][j+1];
27+
}
28+
} else {
29+
if obstacle_grid[i][j] == 1 {
30+
dp[i][j] = 0;
31+
} else {
32+
dp[i][j] = 1;
33+
}
34+
};
35+
} else {
36+
if j == (C - 1) as usize {
37+
if obstacle_grid[i][j] == 1 {
38+
dp[i][j] = 0;
39+
} else {
40+
dp[i][j] = dp[i+1][j];
41+
}
42+
} else {
43+
if obstacle_grid[i][j] == 1 {
44+
dp[i][j] = 0;
45+
} else {
46+
dp[i][j] = dp[i+1][j] + dp[i][j+1];
47+
}
48+
}
49+
}
50+
}
51+
}
52+
53+
dp[0][0]
54+
}
55+
}
56+
// @lc code=end
57+

64.minimum-path-sum.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* @lc app=leetcode id=64 lang=rust
3+
*
4+
* [64] Minimum Path Sum
5+
*/
6+
7+
// @lc code=start
8+
use std::cmp;
9+
10+
impl Solution {
11+
pub fn min_path_sum(grid: Vec<Vec<i32>>) -> i32 {
12+
let R = grid.len();
13+
if R == 0 { return 0; }
14+
15+
let C = grid[0].len();
16+
if C == 0 { return 0; }
17+
18+
19+
let mut dp = vec![vec![1; C as usize]; R as usize];
20+
21+
for i in (0..R as usize).rev() {
22+
for j in (0..C as usize).rev() {
23+
if i == (R - 1) as usize {
24+
if j < (C - 1) as usize {
25+
dp[i][j] = dp[i][j+1] + grid[i][j];
26+
} else {
27+
dp[i][j] = grid[i][j];
28+
};
29+
} else {
30+
if j == (C - 1) as usize {
31+
dp[i][j] = dp[i+1][j] + grid[i][j];
32+
} else {
33+
dp[i][j] = cmp::min(dp[i+1][j], dp[i][j+1]) + grid[i][j];
34+
}
35+
}
36+
}
37+
}
38+
39+
dp[0][0]
40+
}
41+
}
42+
// @lc code=end
43+

0 commit comments

Comments
 (0)