Skip to content

Commit 33eb153

Browse files
committed
solve #94 and add binary-tree util
1 parent a796dc0 commit 33eb153

File tree

7 files changed

+260
-0
lines changed

7 files changed

+260
-0
lines changed

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,7 @@ mod n0087_scramble_string;
9191
mod n0088_merge_sorted_array;
9292
mod n0089_gray_code;
9393
mod n0090_subsets_ii;
94+
mod n0091_decode_ways;
95+
mod n0092_reverse_linked_list_ii;
96+
mod n0093_restore_ip_addresses;
97+
mod n0094_binary_tree_inorder_traversal;

src/n0091_decode_ways.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* [91] Decode Ways
3+
*
4+
* A message containing letters from A-Z is being encoded to numbers using the following mapping:
5+
*
6+
*
7+
* 'A' -> 1
8+
* 'B' -> 2
9+
* ...
10+
* 'Z' -> 26
11+
*
12+
*
13+
* Given a non-empty string containing only digits, determine the total number of ways to decode it.
14+
*
15+
* Example 1:
16+
*
17+
*
18+
* Input: "12"
19+
* Output: 2
20+
* Explanation: It could be decoded as "AB" (1 2) or "L" (12).
21+
*
22+
*
23+
* Example 2:
24+
*
25+
*
26+
* Input: "226"
27+
* Output: 3
28+
* Explanation: It could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).
29+
*
30+
*/
31+
pub struct Solution {}
32+
33+
// submission codes start here
34+
35+
impl Solution {
36+
pub fn num_decodings(s: String) -> i32 {
37+
0
38+
}
39+
}
40+
41+
// submission codes end
42+
43+
#[cfg(test)]
44+
mod tests {
45+
use super::*;
46+
47+
#[test]
48+
fn test_91() {
49+
}
50+
}

src/n0092_reverse_linked_list_ii.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* [92] Reverse Linked List II
3+
*
4+
* Reverse a linked list from position m to n. Do it in one-pass.
5+
*
6+
* Note: 1 ≤ m ≤ n ≤ length of list.
7+
*
8+
* Example:
9+
*
10+
*
11+
* Input: 1->2->3->4->5->NULL, m = 2, n = 4
12+
* Output: 1->4->3->2->5->NULL
13+
*
14+
*
15+
*/
16+
pub struct Solution {}
17+
use super::util::linked_list::{ListNode, to_list};
18+
19+
// submission codes start here
20+
21+
// Definition for singly-linked list.
22+
// #[derive(PartialEq, Eq, Debug)]
23+
// pub struct ListNode {
24+
// pub val: i32,
25+
// pub next: Option<Box<ListNode>>
26+
// }
27+
//
28+
// impl ListNode {
29+
// #[inline]
30+
// fn new(val: i32) -> Self {
31+
// ListNode {
32+
// next: None,
33+
// val
34+
// }
35+
// }
36+
// }
37+
impl Solution {
38+
pub fn reverse_between(head: Option<Box<ListNode>>, m: i32, n: i32) -> Option<Box<ListNode>> {
39+
None
40+
}
41+
}
42+
43+
// submission codes end
44+
45+
#[cfg(test)]
46+
mod tests {
47+
use super::*;
48+
49+
#[test]
50+
fn test_92() {
51+
}
52+
}

src/n0093_restore_ip_addresses.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* [93] Restore IP Addresses
3+
*
4+
* Given a string containing only digits, restore it by returning all possible valid IP address combinations.
5+
*
6+
* Example:
7+
*
8+
*
9+
* Input: "25525511135"
10+
* Output: ["255.255.11.135", "255.255.111.35"]
11+
*
12+
*
13+
*/
14+
pub struct Solution {}
15+
16+
// submission codes start here
17+
18+
impl Solution {
19+
pub fn restore_ip_addresses(s: String) -> Vec<String> {
20+
vec![]
21+
}
22+
}
23+
24+
// submission codes end
25+
26+
#[cfg(test)]
27+
mod tests {
28+
use super::*;
29+
30+
#[test]
31+
fn test_93() {
32+
}
33+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* [94] Binary Tree Inorder Traversal
3+
*
4+
* Given a binary tree, return the inorder traversal of its nodes' values.
5+
*
6+
* Example:
7+
*
8+
*
9+
* Input: [1,null,2,3]
10+
* 1
11+
* \
12+
* 2
13+
* /
14+
* 3
15+
*
16+
* Output: [1,3,2]
17+
*
18+
* Follow up: Recursive solution is trivial, could you do it iteratively?
19+
*
20+
*/
21+
pub struct Solution {}
22+
23+
// submission codes start here
24+
25+
use super::util::tree::{TreeNode, to_tree};
26+
use std::rc::Rc;
27+
use std::cell::RefCell;
28+
impl Solution {
29+
pub fn inorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
30+
let mut res = Vec::new();
31+
Solution::inorder_traverse(root.as_ref(), &mut (|v| {res.push(v)}));
32+
res
33+
}
34+
35+
fn inorder_traverse<F: FnMut(i32)>(root: Option<&Rc<RefCell<TreeNode>>>, consumer: &mut F) {
36+
if let Some(node) = root {
37+
Solution::inorder_traverse(node.borrow().left.as_ref(), consumer);
38+
consumer(root.as_ref().unwrap().borrow().val);
39+
Solution::inorder_traverse(node.borrow().right.as_ref(), consumer);
40+
}
41+
}
42+
}
43+
44+
// submission codes end
45+
46+
#[cfg(test)]
47+
mod tests {
48+
use super::*;
49+
50+
#[test]
51+
fn test_94() {
52+
assert_eq!(
53+
Solution::inorder_traversal(tree![1,null,2,3]),
54+
vec![1,3,2]
55+
);
56+
assert_eq!(
57+
Solution::inorder_traversal(tree![1,2,3,4,5,6,7]),
58+
vec![4,2,5,1,6,3,7]
59+
);
60+
}
61+
}

src/util/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
pub mod linked_list;
33
#[macro_use]
44
pub mod vec_string;
5+
#[macro_use]
6+
pub mod tree;

src/util/tree.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
use std::rc::Rc;
2+
use std::cell::RefCell;
3+
4+
#[derive(Debug, PartialEq, Eq)]
5+
pub struct TreeNode {
6+
pub val: i32,
7+
pub left: Option<Rc<RefCell<TreeNode>>>,
8+
pub right: Option<Rc<RefCell<TreeNode>>>,
9+
}
10+
11+
impl TreeNode {
12+
#[inline]
13+
pub fn new(val: i32) -> Self {
14+
TreeNode {
15+
val,
16+
left: None,
17+
right: None
18+
}
19+
}
20+
}
21+
22+
pub fn to_tree(vec: Vec<Option<i32>>) -> Option<Rc<RefCell<TreeNode>>> {
23+
use std::collections::VecDeque;
24+
let head = Some(Rc::new(RefCell::new(TreeNode::new(vec[0].unwrap()))));
25+
let mut queue = VecDeque::new();
26+
queue.push_back(head.as_ref().unwrap().clone());
27+
28+
for children in vec[1..].chunks(2) {
29+
let parent = queue.pop_front().unwrap();
30+
if let Some(v) = children[0] {
31+
parent.borrow_mut().left = Some(Rc::new(RefCell::new(TreeNode::new(v))));
32+
queue.push_back(parent.borrow().left.as_ref().unwrap().clone());
33+
}
34+
if children.len() > 1 {
35+
if let Some(v) = children[1] {
36+
parent.borrow_mut().right = Some(Rc::new(RefCell::new(TreeNode::new(v))));
37+
queue.push_back(parent.borrow().right.as_ref().unwrap().clone());
38+
}
39+
}
40+
}
41+
head
42+
}
43+
44+
#[macro_export]
45+
macro_rules! tree {
46+
() => {
47+
None
48+
};
49+
($($e:expr),*) => {
50+
{
51+
let vec = vec![$(stringify!($e)), *];
52+
let vec = vec.into_iter().map(|v| v.parse::<i32>().ok()).collect::<Vec<_>>();
53+
to_tree(vec)
54+
}
55+
};
56+
($($e:expr,)*) => {(tree![$($e),*])};
57+
}
58+

0 commit comments

Comments
 (0)