Skip to content

Commit 9b3cdbf

Browse files
committed
More solutions
1 parent d4bec78 commit 9b3cdbf

5 files changed

+175
-2
lines changed

264.ugly-number-ii.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,26 @@
77
// @lc code=start
88
impl Solution {
99
pub fn nth_ugly_number(n: i32) -> i32 {
10-
10+
if n <= 0 { return -1; }
11+
let mut ugly_numbers = vec![1; n as usize];
12+
let mut i2 = 0;
13+
let mut i3 = 0;
14+
let mut i5 = 0;
15+
16+
for i in 1..n as usize {
17+
ugly_numbers[i] = *[
18+
ugly_numbers[i2] * 2,
19+
ugly_numbers[i3] * 3,
20+
ugly_numbers[i5] * 5
21+
].iter().min().unwrap();
22+
23+
if ugly_numbers[i] == ugly_numbers[i2] * 2 { i2 += 1; }
24+
if ugly_numbers[i] == ugly_numbers[i3] * 3 { i3 += 1; }
25+
if ugly_numbers[i] == ugly_numbers[i5] * 5 { i5 += 1; }
26+
}
27+
28+
ugly_numbers[n as usize - 1]
1129
}
1230
}
1331
// @lc code=end
14-
32+

279.perfect-squares.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* @lc app=leetcode id=279 lang=rust
3+
*
4+
* [279] Perfect Squares
5+
*/
6+
7+
// @lc code=start
8+
impl Solution {
9+
pub fn num_squares(n: i32) -> i32 {
10+
11+
}
12+
}
13+
// @lc code=end
14+

599.minimum-index-sum-of-two-lists.rs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* @lc app=leetcode id=599 lang=rust
3+
*
4+
* [599] Minimum Index Sum of Two Lists
5+
*/
6+
7+
// @lc code=start
8+
use std::collections::HashMap;
9+
10+
impl Solution {
11+
pub fn find_restaurant(list1: Vec<String>, list2: Vec<String>) -> Vec<String> {
12+
let mut str_2_idx = HashMap::new();
13+
14+
for (i, s) in list1.into_iter().enumerate() {
15+
if !str_2_idx.contains_key(&s) {
16+
str_2_idx.insert(s, i);
17+
}
18+
}
19+
20+
let mut ans = HashMap::new();
21+
22+
for (i, s) in list2.into_iter().enumerate() {
23+
match str_2_idx.get(&s) {
24+
Some(j) => {
25+
ans.entry(i + *j).or_insert(vec![]).push(s);
26+
},
27+
None => ()
28+
}
29+
}
30+
31+
ans.get(ans.keys().min().unwrap()).unwrap().to_vec()
32+
}
33+
}
34+
// @lc code=end
35+
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* @lc app=leetcode id=606 lang=rust
3+
*
4+
* [606] Construct String from 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 tree2str(t: Option<Rc<RefCell<TreeNode>>>) -> String {
30+
match t {
31+
None => "".to_owned(),
32+
Some(rc_node) => {
33+
let tree_node = rc_node.borrow();
34+
let left = Self::tree2str(tree_node.left.clone());
35+
let right = Self::tree2str(tree_node.right.clone());
36+
37+
let mut ans = String::new();
38+
ans.push_str(&tree_node.val.to_string());
39+
40+
if left != "" || right != "" {
41+
ans.push('(');
42+
ans.push_str(&left);
43+
ans.push(')');
44+
}
45+
46+
if right != "" {
47+
ans.push('(');
48+
ans.push_str(&right);
49+
ans.push(')');
50+
}
51+
52+
ans
53+
}
54+
}
55+
}
56+
}
57+
// @lc code=end
58+

617.merge-two-binary-trees.rs

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* @lc app=leetcode id=617 lang=rust
3+
*
4+
* [617] Merge Two Binary Trees
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 merge_trees(t1: Option<Rc<RefCell<TreeNode>>>, t2: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
30+
match (t1, t2) {
31+
(Some(rn1), Some(rn2)) => {
32+
let tn1 = rn1.borrow();
33+
let tn2 = rn2.borrow();
34+
35+
let mut tn = TreeNode::new(tn1.val + tn2.val);
36+
tn.left = Self::merge_trees(tn1.left.clone(), tn2.left.clone());
37+
tn.right = Self::merge_trees(tn1.right.clone(), tn2.right.clone());
38+
39+
Some(Rc::new(RefCell::new(tn)))
40+
},
41+
(Some(rn1), None) => Some(rn1.clone()),
42+
(None, Some(rn2)) => Some(rn2.clone()),
43+
(None, None) => None,
44+
}
45+
}
46+
}
47+
// @lc code=end
48+

0 commit comments

Comments
 (0)