Skip to content

Commit 3b41d0f

Browse files
committed
11/30/2020
1 parent 581e232 commit 3b41d0f

9 files changed

+301
-47
lines changed

src/_0091_decode_ways.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn num_decodings(s: String) -> i32 {
5+
let n = s.len();
6+
let s_bytes = s.as_bytes();
7+
let mut dp: Vec<i32> = vec![0; n + 1];
8+
dp[0] = 1;
9+
10+
for (i, b) in s_bytes.iter().enumerate() {
11+
if b - '0' as u8 > 0 {
12+
dp[i + 1] += dp[i];
13+
}
14+
if i == 0 {
15+
continue;
16+
}
17+
if (s_bytes[i - 1] - '0' as u8) * 10 + b - '0' as u8 >= 10
18+
&& (s_bytes[i - 1] - '0' as u8) * 10 + b - '0' as u8 <= 26
19+
{
20+
dp[i + 1] += dp[i - 1];
21+
}
22+
}
23+
dp[n]
24+
}
25+
}
26+
27+
#[test]
28+
fn test() {
29+
assert_eq!(Solution::num_decodings("12".to_string()), 2);
30+
assert_eq!(Solution::num_decodings("226".to_string()), 3);
31+
assert_eq!(Solution::num_decodings("0".to_string()), 0);
32+
assert_eq!(Solution::num_decodings("1".to_string()), 1);
33+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use crate::util::*;
2+
use std::cell::RefCell;
3+
use std::rc::Rc;
4+
5+
struct Solution;
6+
7+
impl Solution {
8+
pub fn zigzag_level_order(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<Vec<i32>> {
9+
let mut result: Vec<Vec<i32>> = vec![];
10+
if root.is_none() {
11+
return result;
12+
}
13+
let mut order_lr: bool = true;
14+
let mut level_nums: Vec<i32> = vec![];
15+
let mut visiting: Vec<Rc<RefCell<TreeNode>>> = vec![root.unwrap().clone()];
16+
let mut next: Vec<Rc<RefCell<TreeNode>>> = vec![];
17+
18+
while let Some(node) = visiting.pop() {
19+
let node = node.borrow();
20+
level_nums.push(node.val);
21+
if order_lr {
22+
if let Some(node_left) = &node.left {
23+
next.push(node_left.clone());
24+
}
25+
if let Some(node_right) = &node.right {
26+
next.push(node_right.clone());
27+
}
28+
} else {
29+
if let Some(node_right) = &node.right {
30+
next.push(node_right.clone());
31+
}
32+
if let Some(node_left) = &node.left {
33+
next.push(node_left.clone());
34+
}
35+
}
36+
37+
if visiting.is_empty() {
38+
std::mem::swap(&mut visiting, &mut next);
39+
result.push(level_nums.drain(..).collect());
40+
order_lr = !order_lr;
41+
}
42+
}
43+
44+
result
45+
}
46+
}
47+
48+
#[test]
49+
fn test() {
50+
let root = tree!(3, tree!(9), tree!(20, tree!(15), tree!(7)));
51+
let res = vec_vec_i32![[3], [20, 9], [15, 7]];
52+
assert_eq!(Solution::zigzag_level_order(root), res);
53+
}

src/_0207_course_schedule.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn can_finish(num_courses: i32, prerequisites: Vec<Vec<i32>>) -> bool {
5+
let n = num_courses as usize;
6+
7+
let mut indegrees: Vec<i32> = vec![0; n];
8+
let mut edges: Vec<Vec<usize>> = vec![vec![]; n];
9+
10+
for preq in prerequisites {
11+
let u = preq[0] as usize;
12+
let v = preq[1] as usize;
13+
indegrees[v] += 1;
14+
edges[u].push(v);
15+
}
16+
17+
let mut queue: Vec<usize> = vec![];
18+
for i in 0..n {
19+
if indegrees[i] == 0 {
20+
queue.push(i);
21+
}
22+
}
23+
24+
let mut result: Vec<usize> = vec![];
25+
while let Some(u) = queue.pop() {
26+
result.push(u);
27+
while let Some(v) = edges[u].pop() {
28+
indegrees[v] -= 1;
29+
if indegrees[v] == 0 {
30+
queue.push(v);
31+
}
32+
}
33+
}
34+
result.len() == n
35+
}
36+
}
37+
38+
#[test]
39+
fn test() {
40+
assert_eq!(Solution::can_finish(2, vec_vec_i32![[1, 0]]), true);
41+
assert_eq!(Solution::can_finish(2, vec_vec_i32![[1, 0], [0, 1]]), false);
42+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
use std::collections::HashMap;
2+
3+
struct Trie {
4+
children: HashMap<char, Trie>,
5+
end: bool,
6+
}
7+
impl Trie {
8+
fn new() -> Self {
9+
Trie {
10+
children: HashMap::new(),
11+
end: false,
12+
}
13+
}
14+
15+
fn add(&mut self, word: String) {
16+
let mut node = self;
17+
for c in word.chars() {
18+
node = node.children.entry(c).or_insert(Trie::new());
19+
}
20+
node.end = true;
21+
}
22+
23+
fn search(&self, word: String) -> bool {
24+
if word.is_empty() {
25+
return self.end;
26+
}
27+
let c = word.chars().next().unwrap();
28+
if c == '.' {
29+
for child in self.children.values() {
30+
if Self::search(&child, word[1..].to_string()) {
31+
return true;
32+
}
33+
}
34+
} else {
35+
if let Some(child) = self.children.get(&c) {
36+
return Self::search(&child, word[1..].to_string());
37+
} else {
38+
return false;
39+
}
40+
}
41+
false
42+
}
43+
}
44+
45+
struct WordDictionary {
46+
dictionary: Trie,
47+
}
48+
49+
impl WordDictionary {
50+
fn new() -> Self {
51+
WordDictionary {
52+
dictionary: Trie::new(),
53+
}
54+
}
55+
56+
fn add_word(&mut self, word: String) {
57+
self.dictionary.add(word)
58+
}
59+
60+
fn search(&self, word: String) -> bool {
61+
self.dictionary.search(word)
62+
}
63+
}
64+
65+
#[test]
66+
fn test() {
67+
let mut wd = WordDictionary::new();
68+
wd.add_word("bad".to_string());
69+
wd.add_word("dad".to_string());
70+
wd.add_word("mad".to_string());
71+
assert_eq!(wd.search("pad".to_string()), false);
72+
assert_eq!(wd.search("bad".to_string()), true);
73+
assert_eq!(wd.search(".ad".to_string()), true);
74+
assert_eq!(wd.search("b..".to_string()), true);
75+
}

src/_0337_house_robber_iii.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use crate::util::*;
2+
use std::cell::RefCell;
3+
use std::rc::Rc;
4+
5+
struct Solution;
6+
7+
impl Solution {
8+
pub fn rob(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
9+
let answer = Self::traverse(root.as_ref());
10+
return answer.0.max(answer.1);
11+
}
12+
fn traverse(root: Option<&Rc<RefCell<TreeNode>>>) -> (i32, i32) {
13+
if let Some(root) = root {
14+
let root = root.borrow();
15+
let value = root.val;
16+
let left = Self::traverse(root.left.as_ref());
17+
let right = Self::traverse(root.right.as_ref());
18+
(
19+
value + left.1 + right.1,
20+
left.0.max(left.1) + right.0.max(right.1),
21+
)
22+
} else {
23+
(0, 0)
24+
}
25+
}
26+
}
27+
28+
#[test]
29+
fn test() {
30+
let root = tree!(3, tree!(2, None, tree!(3)), tree!(3, None, tree!(1)));
31+
let res = 7;
32+
assert_eq!(Solution::rob(root), res);
33+
let root = tree!(3, tree!(4, tree!(1), tree!(3)), tree!(5, None, tree!(1)));
34+
let res = 9;
35+
assert_eq!(Solution::rob(root), res);
36+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// struct Solution;
2+
3+
// impl Solution {
4+
// pub fn can_transform(start: String, end: String) -> bool {}
5+
// }
6+
7+
// #[test]
8+
// fn test() {
9+
// assert_eq!(Solution::can_transform(vec![1, 7, 3, 6, 5, 6]), 3);
10+
// }

src/_0875_koko_eating_bananas.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn min_eating_speed(piles: Vec<i32>, h: i32) -> i32 {
5+
let mut left = 1;
6+
let mut right = 1000000001;
7+
while left < right {
8+
let mid = (left + right) / 2;
9+
if Self::can_finish(&piles, h, mid) {
10+
right = mid;
11+
} else {
12+
left = mid + 1;
13+
}
14+
}
15+
left
16+
}
17+
18+
fn can_finish(piles: &Vec<i32>, h: i32, k: i32) -> bool {
19+
let mut hours = 0;
20+
21+
for pile in piles {
22+
if pile % k == 0 {
23+
hours += pile / k;
24+
} else {
25+
hours += pile / k + 1;
26+
}
27+
}
28+
29+
hours <= h
30+
}
31+
}
32+
33+
#[test]
34+
fn test() {
35+
assert_eq!(Solution::min_eating_speed(vec![3, 6, 7, 11], 8), 4);
36+
assert_eq!(Solution::min_eating_speed(vec![30, 11, 23, 4, 20], 5), 30);
37+
assert_eq!(Solution::min_eating_speed(vec![30, 11, 23, 4, 20], 6), 23);
38+
}

src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,12 @@ mod _0083_remove_duplicates_from_sorted_list;
6666
//
6767
mod _0088_merge_sorted_array;
6868
//
69+
mod _0091_decode_ways;
70+
//
6971
mod _0101_symmetric_tree;
7072
//
73+
mod _0103_binary_tree_zigzag_level_order_traversal;
74+
//
7175
mod _0104_maximum_depth_of_binary_tree;
7276
//
7377
mod _0110_balanced_binary_tree;
@@ -102,8 +106,12 @@ mod _0205_isomorphic_strings;
102106
//
103107
mod _0206_reverse_linked_list;
104108
//
109+
mod _0207_course_schedule;
110+
//
105111
mod _0210_course_schedule_ii;
106112
//
113+
mod _0211_add_and_search_word_data_structure_design;
114+
//
107115
mod _0213_house_robber_ii;
108116
//
109117
mod _0215_kth_largest_element_in_an_array;
@@ -138,6 +146,8 @@ mod _0300_longest_increasing_subsequence;
138146
//
139147
mod _0322_coin_change;
140148
//
149+
mod _0337_house_robber_iii;
150+
//
141151
mod _0342_power_of_four;
142152
//
143153
mod _0344_reverse_string;
@@ -212,12 +222,16 @@ mod _0763_partition_labels;
212222
//
213223
mod _0771_jewels_and_stones;
214224
//
225+
mod _0777_swap_adjacent_in_lr_string;
226+
//
215227
mod _0796_rotate_string;
216228
//
217229
mod _0819_most_common_word;
218230
//
219231
mod _0844_backspace_string_compare;
220232
//
233+
mod _0875_koko_eating_bananas;
234+
//
221235
mod _0905_sort_array_by_parity;
222236
//
223237
mod _0922_sort_array_by_parity_ii;

src/test.rs

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1 @@
1-
// use std::io;
2-
// use std::io::BufRead;
3-
// pub fn main() {}
41

5-
// fn main_1() {
6-
// let stdin = io::stdin();
7-
// let mut lines = stdin.lock().lines();
8-
// let line_count = lines.next().unwrap().unwrap();
9-
// let count = line_count.parse::<usize>().unwrap();
10-
// let ratings: Vec<i32> = lines
11-
// .take(count)
12-
// .map(|value| value.unwrap().parse::<i32>().unwrap())
13-
// .collect();
14-
// // let change_count = candies(&ratings);
15-
// // println!("{}", change_count);
16-
// }
17-
18-
// pub fn main_2() {
19-
// // variable declaration
20-
// let mut _num_str_1 = String::new();
21-
// let mut _num_str_2 = String::new();
22-
23-
// // read variables
24-
// io::stdin()
25-
// .read_line(&mut _num_str_1)
26-
// .ok()
27-
// .expect("read error");
28-
// io::stdin()
29-
// .read_line(&mut _num_str_2)
30-
// .ok()
31-
// .expect("read error");
32-
33-
// // parse integers
34-
// let mut _num_1: i32 = _num_str_1.trim().parse().ok().expect("parse error");
35-
// let mut _num_2: i32 = _num_str_2.trim().parse().ok().expect("parse error");
36-
37-
// // print the sum
38-
// // Hint: Type println!("{}", _num_1 + _num_2); below
39-
// println!("{}", _num_1 + _num_2);
40-
// }
41-
42-
// pub fn read_line() -> String {
43-
// let mut input = String::new();
44-
// std::io::stdin()
45-
// .read_line(&mut input)
46-
// .expect("Could not read stdin!");
47-
// return input;
48-
// }

0 commit comments

Comments
 (0)