Skip to content

Commit 5c594fb

Browse files
committed
add 152;162;173;179;213;463;654;792
1 parent 0f5475a commit 5c594fb

8 files changed

+374
-0
lines changed

152.maximum-product-subarray.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* @lc app=leetcode id=152 lang=rust
3+
*
4+
* [152] Maximum Product Subarray
5+
*/
6+
7+
// @lc code=start
8+
use std::cmp;
9+
use std::mem;
10+
11+
impl Solution {
12+
pub fn max_product(nums: Vec<i32>) -> i32 {
13+
let mut imax = nums[0];
14+
let mut imin = nums[0];
15+
16+
let mut ans = nums[0];
17+
18+
for i in 1..nums.len() {
19+
if nums[i] < 0 {
20+
mem::swap(&mut imax, &mut imin);
21+
}
22+
imax = cmp::max(nums[i], imax * nums[i]);
23+
imin = cmp::min(nums[i], imin * nums[i]);
24+
25+
ans = cmp::max(ans, imax);
26+
}
27+
28+
ans
29+
}
30+
}
31+
// @lc code=end
32+

162.find-peak-element.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* @lc app=leetcode id=162 lang=rust
3+
*
4+
* [162] Find Peak Element
5+
*/
6+
7+
// @lc code=start
8+
impl Solution {
9+
pub fn find_peak_element(nums: Vec<i32>) -> i32 {
10+
11+
}
12+
}
13+
// @lc code=end
14+

173.binary-search-tree-iterator.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* @lc app=leetcode id=173 lang=rust
3+
*
4+
* [173] Binary Search Tree Iterator
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+
29+
struct BSTIterator {
30+
stack: Vec<Option<Rc<RefCell<TreeNode>>>>
31+
}
32+
33+
34+
/**
35+
* `&self` means the method takes an immutable reference.
36+
* If you need a mutable reference, change it to `&mut self` instead.
37+
*/
38+
impl BSTIterator {
39+
40+
fn new(mut root: Option<Rc<RefCell<TreeNode>>>) -> Self {
41+
let mut stack = vec![];
42+
43+
while let Some(node) = root.clone() {
44+
stack.push(root.clone());
45+
root = node.borrow().left.clone();
46+
}
47+
48+
BSTIterator { stack }
49+
}
50+
51+
/** @return the next smallest number */
52+
fn next(&mut self) -> i32 {
53+
let mut root = self.stack.pop().unwrap();
54+
let node = root.unwrap();
55+
let ans = node.borrow().val;
56+
root = node.borrow().right.clone();
57+
58+
while let Some(node) = root.clone() {
59+
self.stack.push(root.clone());
60+
root = node.borrow().left.clone();
61+
}
62+
63+
ans
64+
}
65+
66+
/** @return whether we have a next smallest number */
67+
fn has_next(&self) -> bool {
68+
self.stack.len() > 0
69+
}
70+
}
71+
72+
/**
73+
* Your BSTIterator object will be instantiated and called as such:
74+
* let obj = BSTIterator::new(root);
75+
* let ret_1: i32 = obj.next();
76+
* let ret_2: bool = obj.has_next();
77+
*/
78+
// @lc code=end
79+

179.largest-number.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* @lc app=leetcode id=179 lang=rust
3+
*
4+
* [179] Largest Number
5+
*/
6+
7+
// @lc code=start
8+
use std::cmp;
9+
use std::mem;
10+
use std::char;
11+
12+
impl Solution {
13+
pub fn largest_number(nums: Vec<i32>) -> String {
14+
let mut num_str = vec![];
15+
16+
for mut num in nums {
17+
let mut v_num = vec![];
18+
19+
if num == 0 {
20+
v_num.push(0);
21+
} else {
22+
while num > 0 {
23+
v_num.push(num % 10);
24+
num /= 10;
25+
}
26+
}
27+
v_num.reverse();
28+
29+
num_str.push(v_num);
30+
}
31+
32+
fn compare<'a>(a: &'a [i32], b: &'a [i32]) -> cmp::Ordering {
33+
let na = a.len();
34+
let nb = b.len();
35+
36+
let n = cmp::min(na, nb);
37+
38+
for i in 0..n {
39+
if a[i] < b[i] {
40+
return cmp::Ordering::Greater;
41+
} else if a[i] > b[i] {
42+
return cmp::Ordering::Less;
43+
}
44+
}
45+
46+
if na == nb {
47+
cmp::Ordering::Equal
48+
} else if na > nb {
49+
compare(&a[n..], &b[..])
50+
} else {
51+
compare(&a[..], &b[n..])
52+
}
53+
}
54+
55+
num_str.sort_by(|a, b| compare(&a[..], &b[..]));
56+
57+
let mut ans = String::new();
58+
59+
for v in num_str {
60+
for d in v {
61+
ans.push(char::from_digit(d as u32, 10).unwrap());
62+
}
63+
}
64+
65+
let trim_ans = ans.trim_left_matches('0').to_string();
66+
67+
if trim_ans.len() > 0 { trim_ans }
68+
else { "0".to_string() }
69+
}
70+
}
71+
// @lc code=end
72+

213.house-robber-ii.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* @lc app=leetcode id=213 lang=rust
3+
*
4+
* [213] House Robber II
5+
*/
6+
7+
// @lc code=start
8+
impl Solution {
9+
pub fn rob(nums: Vec<i32>) -> i32 {
10+
11+
}
12+
}
13+
// @lc code=end
14+

463.island-perimeter.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* @lc app=leetcode id=463 lang=rust
3+
*
4+
* [463] Island Perimeter
5+
*/
6+
7+
// @lc code=start
8+
use std::collections::HashSet;
9+
10+
impl Solution {
11+
pub fn island_perimeter(grid: Vec<Vec<i32>>) -> i32 {
12+
let R = grid.len();
13+
if R == 0 { return 0; }
14+
let C = grid[0].len();
15+
let mut seen = HashSet::new();
16+
let mut ans = 0;
17+
18+
fn dfs(
19+
coord: (usize, usize),
20+
seen: &mut HashSet<(usize, usize)>,
21+
R: usize, C: usize,
22+
ans: &mut u32,
23+
grid: &Vec<Vec<i32>>
24+
) {
25+
seen.insert(coord);
26+
// println!("{:?}", coord);
27+
// *n_nodes += 1;
28+
let (x, y) = coord;
29+
30+
let mut neis = vec![];
31+
if x > 0 {
32+
neis.push((x-1, y));
33+
} else { *ans += 1; }
34+
35+
if x < R - 1 {
36+
neis.push((x+1, y));
37+
} else { *ans += 1; }
38+
39+
if y > 0 {
40+
neis.push((x, y-1));
41+
} else { *ans += 1; }
42+
43+
if y < C - 1 {
44+
neis.push((x, y+1));
45+
} else { *ans += 1; }
46+
47+
for (i, j) in neis {
48+
// println!("seen {:?} contains {:?}? {}", seen, (i, j), seen.contains(&(i, j)));
49+
if grid[i][j] == 1 {
50+
if !seen.contains(&(i, j)) {
51+
dfs((i, j), seen, R, C, ans, grid);
52+
}
53+
} else {
54+
*ans += 1;
55+
}
56+
}
57+
}
58+
59+
for i in 0..R {
60+
for j in 0..C {
61+
if grid[i][j] == 1 {
62+
if !seen.contains(&(i, j)) {
63+
dfs((i, j), &mut seen, R, C, &mut ans, &grid);
64+
}
65+
}
66+
}
67+
}
68+
69+
// println!("{}, {}", n_nodes, n_edges);
70+
ans as i32
71+
}
72+
}
73+
// @lc code=end
74+

654.maximum-binary-tree.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* @lc app=leetcode id=654 lang=rust
3+
*
4+
* [654] Maximum Binary Tree
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 construct_maximum_binary_tree(nums: Vec<i32>) -> Option<Rc<RefCell<TreeNode>>> {
30+
fn helper(nums: &[i32]) -> Option<Rc<RefCell<TreeNode>>> {
31+
if nums.len() == 0 { return None; }
32+
33+
let mut maxidx = 0;
34+
for i in 0..nums.len() {
35+
if nums[i] > nums[maxidx] {
36+
maxidx = i;
37+
}
38+
}
39+
40+
let mut tree_node = TreeNode::new(nums[maxidx]);
41+
tree_node.left = helper(&nums[..maxidx]);
42+
tree_node.right = helper(&nums[maxidx+1..]);
43+
44+
Some(Rc::new(RefCell::new(tree_node)))
45+
}
46+
47+
helper(&nums[..])
48+
}
49+
}
50+
// @lc code=end
51+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* @lc app=leetcode id=792 lang=rust
3+
*
4+
* [792] Number of Matching Subsequences
5+
*/
6+
7+
// @lc code=start
8+
impl Solution {
9+
pub fn num_matching_subseq(s: String, words: Vec<String>) -> i32 {
10+
fn is_subseq(word1: &str, word2: &str) -> bool {
11+
let mut iter1 = word1.chars();
12+
let mut iter2 = word2.chars();
13+
14+
while let Some(ch1) = iter1.next() {
15+
loop {
16+
if let Some(ch2) = iter2.next() {
17+
if ch1 == ch2 { break; }
18+
} else {
19+
return false;
20+
}
21+
}
22+
}
23+
24+
return true;
25+
}
26+
27+
let mut ans = 0;
28+
for word2 in words {
29+
if is_subseq(&word2, &s) {
30+
ans += 1;
31+
}
32+
}
33+
34+
ans
35+
}
36+
}
37+
// @lc code=end
38+

0 commit comments

Comments
 (0)