Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5524223

Browse files
committed
Solve #241
1 parent 40c3668 commit 5524223

File tree

3 files changed

+106
-18
lines changed

3 files changed

+106
-18
lines changed

‎src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,4 @@ mod n0232_implement_queue_using_stacks;
203203
mod n0233_number_of_digit_one;
204204
mod n0238_product_of_array_except_self;
205205
mod n0239_sliding_window_maximum;
206+
mod n0241_different_ways_to_add_parentheses;

‎src/n0106_construct_binary_tree_from_inorder_and_postorder_traversal.rs‎

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,54 @@
22
* [106] Construct Binary Tree from Inorder and Postorder Traversal
33
*
44
* Given inorder and postorder traversal of a tree, construct the binary tree.
5-
*
5+
*
66
* Note:<br />
77
* You may assume that duplicates do not exist in the tree.
8-
*
8+
*
99
* For example, given
10-
*
11-
*
10+
*
11+
*
1212
* inorder = [9,3,15,20,7]
1313
* postorder = [9,15,7,20,3]
14-
*
14+
*
1515
* Return the following binary tree:
16-
*
17-
*
16+
*
17+
*
1818
* 3
1919
* / \
2020
* 9 20
2121
* / \
2222
* 15 7
23-
*
24-
*
23+
*
24+
*
2525
*/
2626
pub struct Solution {}
27-
use super::util::tree::{TreeNode, to_tree};
27+
use super::util::tree::{to_tree,TreeNode};
2828

2929
// submission codes start here
3030

31-
use std::rc::Rc;
3231
use std::cell::RefCell;
32+
use std::rc::Rc;
3333
impl Solution {
3434
pub fn build_tree(inorder: Vec<i32>, postorder: Vec<i32>) -> Option<Rc<RefCell<TreeNode>>> {
35-
Solution::build_tree_helper(&postorder[..], &inorder[..],)
35+
Solution::build_tree_helper(&postorder[..], &inorder[..])
3636
}
3737

3838
fn build_tree_helper(postorder: &[i32], inorder: &[i32]) -> Option<Rc<RefCell<TreeNode>>> {
39-
if postorder.is_empty() { return None }
40-
let root_idx = inorder.iter().position(|v| v == postorder.last().unwrap()).unwrap();
41-
Some(Rc::new(RefCell::new(TreeNode{
39+
if postorder.is_empty() {
40+
return None;
41+
}
42+
let root_idx = inorder
43+
.iter()
44+
.position(|v| v == postorder.last().unwrap())
45+
.unwrap();
46+
Some(Rc::new(RefCell::new(TreeNode {
4247
val: *postorder.last().unwrap(),
4348
left: Solution::build_tree_helper(&postorder[0..root_idx], &inorder[0..root_idx]),
44-
right: Solution::build_tree_helper(&postorder[root_idx..postorder.len()-1], &inorder[root_idx+1..]),
49+
right: Solution::build_tree_helper(
50+
&postorder[root_idx..postorder.len() - 1],
51+
&inorder[root_idx + 1..],
52+
),
4553
})))
4654
}
4755
}
@@ -54,8 +62,14 @@ mod tests {
5462

5563
#[test]
5664
fn test_106() {
57-
assert_eq!(Solution::build_tree(vec![9,3,15,20,7], vec![9,15,7,20,3]), tree![3,9,20,null,null,15,7]);
58-
assert_eq!(Solution::build_tree(vec![3,20,7], vec![7,20,3]), tree![3,null,20,null,7]);
65+
assert_eq!(
66+
Solution::build_tree(vec![9, 3, 15, 20, 7], vec![9, 15, 7, 20, 3]),
67+
tree![3, 9, 20, null, null, 15, 7]
68+
);
69+
assert_eq!(
70+
Solution::build_tree(vec![3, 20, 7], vec![7, 20, 3]),
71+
tree![3, null, 20, null, 7]
72+
);
5973
assert_eq!(Solution::build_tree(vec![], vec![]), tree![]);
6074
}
6175
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* [241] Different Ways to Add Parentheses
3+
*
4+
* Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are +, - and *.
5+
*
6+
* Example 1:
7+
*
8+
*
9+
* Input: "2-1-1"
10+
* Output: [0, 2]
11+
* Explanation:
12+
* ((2-1)-1) = 0
13+
* (2-(1-1)) = 2
14+
*
15+
* Example 2:
16+
*
17+
*
18+
* Input: "2*3-4*5"
19+
* Output: [-34, -14, -10, -10, 10]
20+
* Explanation:
21+
* (2*(3-(4*5))) = -34
22+
* ((2*3)-(4*5)) = -14
23+
* ((2*(3-4))*5) = -10
24+
* (2*((3-4)*5)) = -10
25+
* (((2*3)-4)*5) = 10
26+
*
27+
*/
28+
pub struct Solution {}
29+
30+
// submission codes start here
31+
32+
impl Solution {
33+
pub fn diff_ways_to_compute(input: String) -> Vec<i32> {
34+
Solution::helper(&input)
35+
}
36+
37+
pub fn helper(input: &str) -> Vec<i32> {
38+
if input.is_empty() { return vec![] }
39+
if let Ok(digit) = input.parse::<i32>() {
40+
return vec![digit]
41+
}
42+
let mut res: Vec<i32> = Vec::new();
43+
for (i, ch) in input.chars().enumerate() {
44+
if ch == '+' || ch == '-' || ch == '*' {
45+
let left = Solution::helper(&input[..i]);
46+
let right = Solution::helper(&input[i+1..]);
47+
for &a in left.iter() {
48+
for &b in right.iter() {
49+
res.push(match ch {
50+
'+' => a + b,
51+
'-' => a - b,
52+
'*' => a * b,
53+
_ => unreachable!(),
54+
})
55+
}
56+
}
57+
}
58+
}
59+
res
60+
}
61+
}
62+
63+
// submission codes end
64+
65+
#[cfg(test)]
66+
mod tests {
67+
use super::*;
68+
69+
#[test]
70+
fn test_241() {
71+
assert_eq!(Solution::diff_ways_to_compute("2*3-4*5".to_owned()), vec![-34, -10, -14, -10, 10]);
72+
}
73+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /