Skip to content

Commit 023408f

Browse files
committed
2019-11-25
1 parent 5c594fb commit 023408f

22 files changed

+612
-13
lines changed

10.regular-expression-matching.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,15 @@
77
// @lc code=start
88
impl Solution {
99
pub fn is_match(s: String, p: String) -> bool {
10+
let sn = s.len();
11+
let pn = p.len();
12+
1013

14+
for i in 0..pn {
15+
if i < pn - 1 {
16+
17+
}
18+
}
1119
}
1220
}
1321
// @lc code=end

162.find-peak-element.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77
// @lc code=start
88
impl Solution {
99
pub fn find_peak_element(nums: Vec<i32>) -> i32 {
10-
10+
let mut lo = 0;
11+
let mut hi = nums.len() - 1;
12+
13+
while lo < hi {
14+
let mut mid = (lo + hi) / 2;
15+
16+
}
1117
}
1218
}
1319
// @lc code=end

17.letter-combinations-of-a-phone-number.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ impl Solution {
1212
if digits.len() == 0 {
1313
return vec![];
1414
}
15+
1516
let mut ans = vec!["".to_string()];
1617
let map: HashMap<char, Vec<char>> = [
1718
('2', vec!['a', 'b', 'c']),

190.reverse-bits.rs

Whitespace-only changes.

213.house-robber-ii.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,28 @@
55
*/
66

77
// @lc code=start
8+
use std::cmp;
9+
810
impl Solution {
911
pub fn rob(nums: Vec<i32>) -> i32 {
10-
12+
13+
let mut rob0_robi = nums[0];
14+
let mut rob0_norobi = 0;
15+
let mut norob0_robi = 0;
16+
let mut norob0_norobi = 0;
17+
18+
let mut rob = nums[0];
19+
let mut norob = 0;
20+
21+
let mut money = (nums[0], 0, 0, 0);
22+
23+
let N = nums.len();
24+
25+
for i in 1..N-1 {
26+
let tmp = rob;
27+
rob = norob + nums[i];
28+
norob = cmp::max(rob, norob);
29+
}
1130
}
1231
}
1332
// @lc code=end

234.palindrome-linked-list.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,53 @@
2222
// }
2323
// }
2424
impl Solution {
25-
pub fn is_palindrome(head: Option<Box<ListNode>>) -> bool {
25+
pub fn is_palindrome(mut head: Option<Box<ListNode>>) -> bool {
26+
if head.is_none() { return true; }
2627

28+
let mut N = 0;
29+
let mut href = head.as_ref();
30+
31+
while let Some(node) = href {
32+
href = node.next.as_ref();
33+
N += 1;
34+
}
35+
36+
let mut hmut = head.as_mut();
37+
let mut mid = N / 2 - 1;
38+
39+
while mid > 0 {
40+
hmut = hmut.unwrap().next.as_mut();
41+
mid -= 1;
42+
}
43+
44+
let mut prev = None;
45+
let mut next = hmut.unwrap().next.take();
46+
47+
while let Some(mut next_inner) = next {
48+
next = next_inner.next.take();
49+
next_inner.next = prev.take();
50+
prev = Some(next_inner);
51+
}
52+
53+
let mut h1 = &head;
54+
let mut h2 = &prev;
55+
56+
loop {
57+
let (not_equal, should_break) = match (h1.as_ref(), h2.as_ref()) {
58+
(Some(h1_inner), Some(h2_inner)) => {
59+
h1 = &h1_inner.next;
60+
h2 = &h2_inner.next;
61+
(h1_inner.val != h2_inner.val, false)
62+
},
63+
_ => (false, true)
64+
};
65+
66+
if should_break { break; }
67+
if not_equal { return false; }
68+
}
69+
70+
return true;
71+
2772
}
2873
}
2974
// @lc code=end

290.word-pattern.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* @lc app=leetcode id=290 lang=rust
3+
*
4+
* [290] Word Pattern
5+
*/
6+
7+
// @lc code=start
8+
use std::collections::HashMap;
9+
10+
impl Solution {
11+
pub fn word_pattern(pattern: String, str: String) -> bool {
12+
let mut forward = HashMap::new();
13+
let mut backward = HashMap::new();
14+
15+
let words: Vec<&str> = str.split(' ').collect();
16+
if pattern.len() != words.len() {
17+
return false;
18+
}
19+
for (i, ch) in pattern.chars().enumerate() {
20+
if forward.contains_key(&ch) {
21+
if forward.get(&ch).unwrap() != words[i] {
22+
return false;
23+
}
24+
} else {
25+
forward.insert(ch, words[i].to_owned());
26+
}
27+
28+
if backward.contains_key(words[i]) {
29+
if backward.get(words[i]).unwrap() != &ch {
30+
return false;
31+
}
32+
} else {
33+
backward.insert(words[i].to_owned(), ch);
34+
}
35+
}
36+
37+
return true;
38+
}
39+
}
40+
// @lc code=end
41+

31.next-permutation.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=31 lang=rust
3+
*
4+
* [31] Next Permutation
5+
*/
6+
7+
// @lc code=start
8+
impl Solution {
9+
pub fn next_permutation(nums: &mut Vec<i32>) {
10+
let N = nums.len();
11+
12+
let mut i = N - 1;
13+
while i > 0 && nums[i] <= nums[i-1] {
14+
i -= 1;
15+
}
16+
17+
if i == 0 {
18+
nums.sort();
19+
return;
20+
}
21+
22+
let left = i - 1;
23+
24+
let mut right = left + 1;
25+
26+
while right < N && nums[left] < nums[right] {
27+
right += 1;
28+
}
29+
30+
nums.swap(left, right - 1);
31+
32+
nums[left+1..].sort()
33+
}
34+
}
35+
// @lc code=end
36+

336.palindrome-pairs.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,36 @@
55
*/
66

77
// @lc code=start
8+
use std::collections::HashMap;
9+
810
impl Solution {
911
pub fn palindrome_pairs(words: Vec<String>) -> Vec<Vec<i32>> {
10-
12+
words.sort_by_key(|s| s.len());
13+
14+
let mut prefix = HashMap::new();
15+
let mut suffix = HashMap::new();
16+
17+
for i in 0..words.len() {
18+
19+
for
20+
if suffix.contains_key(k: &Q)
21+
22+
let mut pindices = prefix.get_mut(&words[i]);
23+
if let Some(pind) = pindices {
24+
(*pind).push(i);
25+
} else {
26+
*pind = vec![];
27+
}
28+
29+
let mut sindices = suffix.get_mut(&words[i].chars().rev().collect()::<String>);
30+
if let Some(sind) = sindices {
31+
(*sind).push(i);
32+
} else {
33+
*pind = vec![i];
34+
}
35+
36+
37+
}
1138
}
1239
}
1340
// @lc code=end

37.sudoku-solver.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* @lc app=leetcode id=37 lang=rust
3+
*
4+
* [37] Sudoku Solver
5+
*/
6+
7+
// @lc code=start
8+
use std::collections::HashSet;
9+
use std::char;
10+
11+
impl Solution {
12+
pub fn solve_sudoku(board: &mut Vec<Vec<char>>) {
13+
let s: HashSet<char> = (1..10).map(|x| char::from_digit(x, 10).unwrap()).collect();
14+
15+
let mut row_choices = vec![s.clone(); 9];
16+
let mut col_choices = vec![s.clone(); 9];
17+
let mut sqr_choices = vec![s.clone(); 9];
18+
19+
for i in 0..9 {
20+
for j in 0..9 {
21+
if board[i][j] != '.' {
22+
row_choices[i].remove(&board[i][j]);
23+
col_choices[j].remove(&board[i][j]);
24+
sqr_choices[3 * (i / 3) + (j / 3)].remove(&board[i][j]);
25+
}
26+
}
27+
}
28+
29+
// println!("row: {:?}", row_choices);
30+
// println!("col: {:?}", col_choices);
31+
// println!("sqr: {:?}", sqr_choices);
32+
33+
fn back_track(i: usize, j: usize, board: &mut Vec<Vec<char>>,
34+
row_choices: &mut Vec<HashSet<char>>,
35+
col_choices: &mut Vec<HashSet<char>>,
36+
sqr_choices: &mut Vec<HashSet<char>>
37+
) -> bool {
38+
if i == 9 { return true; }
39+
else if board[i][j] == '.' {
40+
41+
for &ch in row_choices[i].clone().intersection(&col_choices[j].clone()) {
42+
let sqr_idx = 3 * (i / 3) + (j / 3);
43+
if sqr_choices[sqr_idx].clone().contains(&ch) {
44+
board[i][j] = ch;
45+
row_choices[i].remove(&ch);
46+
col_choices[j].remove(&ch);
47+
sqr_choices[sqr_idx].remove(&ch);
48+
let res = if j < 8 {
49+
back_track(i, j+1, board, row_choices, col_choices, sqr_choices)
50+
} else {
51+
back_track(i+1, 0, board, row_choices, col_choices, sqr_choices)
52+
};
53+
if res { return true; }
54+
else {
55+
board[i][j] = '.';
56+
row_choices[i].insert(ch);
57+
col_choices[j].insert(ch);
58+
sqr_choices[sqr_idx].insert(ch);
59+
}
60+
}
61+
}
62+
return false;
63+
} else {
64+
let res = if j < 8 {
65+
back_track(i, j+1, board, row_choices, col_choices, sqr_choices)
66+
} else {
67+
back_track(i+1, 0, board, row_choices, col_choices, sqr_choices)
68+
};
69+
70+
return res;
71+
}
72+
}
73+
74+
back_track(0, 0, board, &mut row_choices, &mut col_choices, &mut sqr_choices);
75+
}
76+
}
77+
// @lc code=end
78+

39.combination-sum.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* @lc app=leetcode id=39 lang=rust
3+
*
4+
* [39] Combination Sum
5+
*/
6+
7+
// @lc code=start
8+
impl Solution {
9+
pub fn combination_sum(candidates: Vec<i32>, target: i32) -> Vec<Vec<i32>> {
10+
fn helper(candidates: &[i32], target: i32) -> Vec<Vec<i32>> {
11+
12+
}
13+
}
14+
}
15+
// @lc code=end
16+

0 commit comments

Comments
 (0)