Skip to content

Commit 1fb97c2

Browse files
authored
Merge pull request #40 from jtr109/develop
Develop
2 parents ca9de8f + c207880 commit 1fb97c2

File tree

20 files changed

+831
-0
lines changed

20 files changed

+831
-0
lines changed

Cargo.lock

+36
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+9
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,13 @@ members = [
4343
"maximum_performance_of_a_team",
4444
"longest_consecutive_sequence",
4545
"min_cost_climbing_stairs",
46+
"construct_binary_tree_from_preorder_and_inorder_traversal",
47+
"jump_game_vi",
48+
"my_calendar_i",
49+
"stone_game_vii",
50+
"minumum_number_of_refueling_stops",
51+
"palindrome_pairs",
52+
"matchsticks_to_square",
53+
"generate_parentheses",
54+
"number_of_subarrays_with_bounded_maximum",
4655
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "construct_binary_tree_from_preorder_and_inorder_traversal"
3+
version = "0.1.0"
4+
authors = ["Ryan Li <conbas2019@gmail.com>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
use std::cell::RefCell;
2+
use std::ops::Index;
3+
use std::rc::Rc;
4+
5+
// Definition for a binary tree node.
6+
#[derive(Debug, PartialEq, Eq)]
7+
pub struct TreeNode {
8+
pub val: i32,
9+
pub left: Option<Rc<RefCell<TreeNode>>>,
10+
pub right: Option<Rc<RefCell<TreeNode>>>,
11+
}
12+
13+
impl TreeNode {
14+
#[inline]
15+
pub fn new(val: i32) -> Self {
16+
TreeNode {
17+
val,
18+
left: None,
19+
right: None,
20+
}
21+
}
22+
}
23+
24+
pub struct Solution {}
25+
26+
impl Solution {
27+
pub fn build_tree(preorder: Vec<i32>, inorder: Vec<i32>) -> Option<Rc<RefCell<TreeNode>>> {
28+
match preorder.iter().nth(0) {
29+
None => None,
30+
Some(val) => {
31+
let index = inorder.iter().position(|v| val == v).unwrap();
32+
let left_inorder = inorder[..index].to_vec();
33+
let left_preorder = preorder[1..1 + index].to_vec();
34+
let right_inorder = inorder[index + 1..].to_vec();
35+
let right_preorder = preorder[1 + index..].to_vec();
36+
Some(Rc::new(RefCell::new(TreeNode {
37+
val: *val,
38+
left: Self::build_tree(left_preorder, left_inorder),
39+
right: Self::build_tree(right_preorder, right_inorder),
40+
})))
41+
}
42+
}
43+
}
44+
}
45+
46+
#[cfg(test)]
47+
mod tests {
48+
use super::*;
49+
50+
#[test]
51+
fn example_1() {
52+
let preorder = [3, 9, 20, 15, 7];
53+
let inorder = [9, 3, 15, 20, 7];
54+
let expected = Some(Rc::new(RefCell::new(TreeNode {
55+
val: 3,
56+
left: Some(Rc::new(RefCell::new(TreeNode {
57+
val: 9,
58+
left: None,
59+
right: None,
60+
}))),
61+
right: Some(Rc::new(RefCell::new(TreeNode {
62+
val: 20,
63+
left: Some(Rc::new(RefCell::new(TreeNode {
64+
val: 15,
65+
left: None,
66+
right: None,
67+
}))),
68+
right: Some(Rc::new(RefCell::new(TreeNode {
69+
val: 7,
70+
left: None,
71+
right: None,
72+
}))),
73+
}))),
74+
})));
75+
assert_eq!(
76+
Solution::build_tree(preorder.to_vec(), inorder.to_vec()),
77+
expected
78+
);
79+
}
80+
81+
#[test]
82+
fn example_2() {
83+
let preorder = [-1];
84+
let inorder = [-1];
85+
let expected = Some(Rc::new(RefCell::new(TreeNode::new(-1))));
86+
assert_eq!(
87+
Solution::build_tree(preorder.to_vec(), inorder.to_vec()),
88+
expected
89+
);
90+
}
91+
}

generate_parentheses/Cargo.toml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "generate_parentheses"
3+
version = "0.1.0"
4+
authors = ["Ryan Li <conbas2019@gmail.com>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]

generate_parentheses/src/lib.rs

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*!
2+
* Follow solution: https://leetcode.com/problems/generate-parentheses/discuss/10100/Easy-to-understand-Java-backtracking-solution
3+
*/
4+
5+
pub struct Solution {}
6+
7+
impl Solution {
8+
pub fn generate_parenthesis(n: i32) -> Vec<String> {
9+
let mut list = Vec::new();
10+
Self::backtrack(&mut list, "".to_string(), 0, 0, n as usize);
11+
list
12+
}
13+
14+
fn backtrack(list: &mut Vec<String>, s: String, open: usize, close: usize, max: usize) {
15+
// 如果前缀为 s 的值,获取所有可能性,加入 list
16+
if (s.chars().count()) == max * 2 {
17+
list.push(s);
18+
return;
19+
}
20+
21+
if open < max {
22+
// 如果前缀中的 `(` 数量达到最大值,不再往前缀中加入 `(`
23+
Self::backtrack(list, s.clone() + "(", open + 1, close, max);
24+
}
25+
if close < open {
26+
// 如果前缀中的 `)` 数量和 `(` 数量一样多了,不再往前缀中加入 `)`
27+
Self::backtrack(list, s.clone() + ")", open, close + 1, max);
28+
}
29+
}
30+
}
31+
32+
#[cfg(test)]
33+
mod tests {
34+
use super::*;
35+
36+
fn it_works(n: i32, expected: Vec<&str>) {
37+
let mut result = Solution::generate_parenthesis(n);
38+
let mut exp = expected.to_vec();
39+
result.sort();
40+
exp.sort();
41+
assert_eq!(result, exp);
42+
}
43+
44+
#[test]
45+
fn example_1() {
46+
let n = 3;
47+
let expected = ["((()))", "(()())", "(())()", "()(())", "()()()"];
48+
it_works(n, expected.to_vec());
49+
}
50+
51+
#[test]
52+
fn example_2() {
53+
let n = 1;
54+
let expected = ["()"];
55+
it_works(n, expected.to_vec());
56+
}
57+
58+
#[test]
59+
fn submission_1() {
60+
let n = 4;
61+
let expected = [
62+
"(((())))", "((()()))", "((())())", "((()))()", "(()(()))", "(()()())", "(()())()",
63+
"(())(())", "(())()()", "()((()))", "()(()())", "()(())()", "()()(())", "()()()()",
64+
];
65+
it_works(n, expected.to_vec());
66+
}
67+
}

jump_game_vi/Cargo.toml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "jump_game_vi"
3+
version = "0.1.0"
4+
authors = ["Ryan Li <conbas2019@gmail.com>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]

0 commit comments

Comments
 (0)