Skip to content

Commit d7a1fad

Browse files
committed
add 114,118,155,160,171,189 modified 110
1 parent f2b019a commit d7a1fad

7 files changed

+212
-1
lines changed

110.balanced-binary-tree.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,28 @@
2525
// }
2626
use std::rc::Rc;
2727
use std::cell::RefCell;
28+
use std::cmp;
29+
2830
impl Solution {
2931
pub fn is_balanced(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
30-
32+
fn helper(root: Option<Rc<RefCell<TreeNode>>>) -> (bool, i32) {
33+
if let Some(rc_node) = root {
34+
let tree_node = rc_node.borrow();
35+
let (is_left_balanced, left_height) = helper(tree_node.left.clone());
36+
let (is_right_balanced, right_height) = helper(tree_node.right.clone());
37+
38+
if is_left_balanced && is_right_balanced && (left_height - right_height).abs() <= 1 {
39+
(true, cmp::max(left_height, right_height) + 1)
40+
} else {
41+
(false, 0)
42+
}
43+
} else {
44+
(true, 0)
45+
}
46+
}
47+
48+
let (ans, _) = helper(root);
49+
ans
3150
}
3251
}
3352
// @lc code=end
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* @lc app=leetcode id=114 lang=rust
3+
*
4+
* [114] Flatten Binary Tree to Linked List
5+
*/
6+
7+
// @lc code=start
8+
// Definition for a binary tree node.
9+
// #[derive(Debug, PartialEq, Eq)]
10+
// pub struct TreeNode {
11+
// pub val: i32,
12+
// pub left: Option<Rc<RefCell<TreeNode>>>,
13+
// pub right: Option<Rc<RefCell<TreeNode>>>,
14+
// }
15+
//
16+
// impl TreeNode {
17+
// #[inline]
18+
// pub fn new(val: i32) -> Self {
19+
// TreeNode {
20+
// val,
21+
// left: None,
22+
// right: None
23+
// }
24+
// }
25+
// }
26+
use std::rc::Rc;
27+
use std::cell::RefCell;
28+
impl Solution {
29+
pub fn flatten(root: &mut Option<Rc<RefCell<TreeNode>>>) {
30+
31+
if let Some(rc_node) = root {
32+
Self::flatten(&mut rc_node.borrow_mut().left);
33+
Self::flatten(&mut rc_node.borrow_mut().right);
34+
35+
let mut left = rc_node.borrow_mut().left.take();
36+
if left.is_some() {
37+
let right = rc_node.borrow().right.clone();
38+
rc_node.borrow_mut().right = left.clone();
39+
40+
let mut left_ref = left.clone();
41+
while let Some(next) = left_ref.take() {
42+
if next.borrow().right.is_some() {
43+
left_ref = next.borrow_mut().right.clone();
44+
} else {
45+
next.borrow_mut().right = right;
46+
break;
47+
}
48+
}
49+
}
50+
}
51+
52+
}
53+
}
54+
// @lc code=end
55+

118.pascals-triangle.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* @lc app=leetcode id=118 lang=rust
3+
*
4+
* [118] Pascal's Triangle
5+
*/
6+
7+
// @lc code=start
8+
impl Solution {
9+
pub fn generate(num_rows: i32) -> Vec<Vec<i32>> {
10+
let mut ans = vec![];
11+
let mut curr = vec![1];
12+
13+
for i in 0..num_rows {
14+
let mut next = vec![1];
15+
for i in 0..curr.len()-1 {
16+
next.push(curr[i] + curr[i+1]);
17+
}
18+
next.push(1);
19+
ans.push(curr);
20+
curr = next;
21+
}
22+
23+
ans
24+
}
25+
}
26+
// @lc code=end
27+

155.min-stack.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* @lc app=leetcode id=155 lang=rust
3+
*
4+
* [155] Min Stack
5+
*/
6+
7+
// @lc code=start
8+
// use std::i32;
9+
10+
struct MinStack {
11+
pub stack: Vec<i32>,
12+
pub min: i32
13+
}
14+
15+
16+
/**
17+
* `&self` means the method takes an immutable reference.
18+
* If you need a mutable reference, change it to `&mut self` instead.
19+
*/
20+
impl MinStack {
21+
22+
/** initialize your data structure here. */
23+
fn new() -> Self {
24+
MinStack { stack: vec![], min: std::i32::MAX }
25+
}
26+
27+
fn push(&mut self, x: i32) {
28+
if x <= self.min {
29+
self.stack.push(self.min);
30+
self.min = x;
31+
}
32+
self.stack.push(x);
33+
34+
}
35+
36+
fn pop(&mut self) {
37+
match self.stack.pop() {
38+
None => None,
39+
Some(val) => {
40+
if val == self.min {
41+
match self.stack.pop() {
42+
None => { self.min = std::i32::MAX; },
43+
Some(val1) => { self.min = val1; }
44+
};
45+
}
46+
Some(val)
47+
}
48+
};
49+
}
50+
51+
fn top(&self) -> i32 {
52+
self.stack[self.stack.len()-1]
53+
}
54+
55+
fn get_min(&self) -> i32 {
56+
self.min
57+
}
58+
}
59+
60+
/**
61+
* Your MinStack object will be instantiated and called as such:
62+
* let obj = MinStack::new();
63+
* obj.push(x);
64+
* obj.pop();
65+
* let ret_3: i32 = obj.top();
66+
* let ret_4: i32 = obj.get_min();
67+
*/
68+
// @lc code=end
69+

160.intersection-of-two-linked-lists.rs

Whitespace-only changes.

171.excel-sheet-column-number.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* @lc app=leetcode id=171 lang=rust
3+
*
4+
* [171] Excel Sheet Column Number
5+
*/
6+
7+
// @lc code=start
8+
impl Solution {
9+
pub fn title_to_number(s: String) -> i32 {
10+
let mut ans = 0;
11+
12+
for c in s.chars() {
13+
ans *= 26;
14+
ans += c as i32 - 'A' as i32 + 1;
15+
}
16+
17+
ans
18+
}
19+
}
20+
// @lc code=end
21+

189.rotate-array.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* @lc app=leetcode id=189 lang=rust
3+
*
4+
* [189] Rotate Array
5+
*/
6+
7+
// @lc code=start
8+
impl Solution {
9+
pub fn rotate(nums: &mut Vec<i32>, k: i32) {
10+
let mut ans = vec![0; nums.len()];
11+
12+
for i in 0..nums.len() {
13+
ans[(i+k as usize)%nums.len()] = nums[i];
14+
}
15+
16+
ans
17+
}
18+
}
19+
// @lc code=end
20+

0 commit comments

Comments
 (0)