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 96f5b5b

Browse files
committed
Add more solutions
1 parent 74e0b6f commit 96f5b5b

File tree

6 files changed

+263
-111
lines changed

6 files changed

+263
-111
lines changed

‎README.md

Lines changed: 116 additions & 110 deletions
Large diffs are not rendered by default.

‎src/leetcode/problem/is_same_tree.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ impl TreeNode {
1414
use std::cell::RefCell;
1515
use std::rc::Rc;
1616

17-
// 100. Same Tree, easy
17+
// 100. Same Tree, Easy
1818
// https://leetcode.com/problems/same-tree/
1919
impl Solution {
2020
pub fn is_same_tree(p: Option<Rc<RefCell<TreeNode>>>, q: Option<Rc<RefCell<TreeNode>>>) -> bool {

‎src/leetcode/problem/path_sum_3.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Definition for a binary tree node.
2+
#[derive(Debug, PartialEq, Eq)]
3+
pub struct TreeNode {
4+
pub val: i32,
5+
pub left: Option<Rc<RefCell<TreeNode>>>,
6+
pub right: Option<Rc<RefCell<TreeNode>>>,
7+
}
8+
9+
impl TreeNode {
10+
#[inline]
11+
pub fn new(val: i32) -> Self {
12+
TreeNode { val, left: None, right: None }
13+
}
14+
}
15+
16+
use std::cell::RefCell;
17+
use std::rc::Rc;
18+
19+
// 437. Path Sum III, Medium
20+
// https://leetcode.com/problems/path-sum-iii/
21+
impl Solution {
22+
pub fn path_sum(root: Option<Rc<RefCell<TreeNode>>>, target_sum: i32) -> i32 {
23+
fn bfs(node: Option<Rc<RefCell<TreeNode>>>, sums: Vec<i32>, target_sum: i32, res: &mut i32) {
24+
if let Some(node) = node {
25+
let node = node.borrow_mut();
26+
let mut new_sums = Vec::new();
27+
new_sums.push(node.val);
28+
for sum in sums {
29+
new_sums.push(sum + node.val);
30+
if sum + node.val == target_sum {
31+
*res += 1;
32+
}
33+
}
34+
if node.val == target_sum {
35+
*res += 1;
36+
}
37+
38+
bfs(node.left.clone(), new_sums.clone(), target_sum, res);
39+
bfs(node.right.clone(), new_sums.clone(), target_sum, res);
40+
}
41+
}
42+
43+
let mut res = 0;
44+
bfs(root, vec![], target_sum, &mut res);
45+
res
46+
}
47+
}
48+
49+
struct Solution {}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// 93. Restore IP Addresses, Medium
2+
// https://leetcode.com/problems/restore-ip-addresses/
3+
impl Solution {
4+
pub fn restore_ip_addresses(s: String) -> Vec<String> {
5+
let digits = s.chars().map(|c| c.to_digit(10).unwrap()).collect::<Vec<u32>>();
6+
7+
fn helper(blocks: Vec<Vec<u32>>, digits: &Vec<u32>, i: usize, ans: &mut Vec<String>) {
8+
if blocks.len() > 4 || i > digits.len() {
9+
return;
10+
} else if blocks.len() == 4 && i == digits.len() {
11+
let mut s = String::new();
12+
for block in blocks {
13+
for d in block {
14+
s.push_str(&d.to_string());
15+
}
16+
s.push('.');
17+
}
18+
s.pop();
19+
ans.push(s);
20+
} else {
21+
let mut block = vec![];
22+
let mut num = 0;
23+
for j in i..usize::min(digits.len(), i + 3) {
24+
block.push(digits[j]);
25+
num = num * 10 + digits[j];
26+
if num > 255 || (block.first().unwrap() == &0 && block.len() > 1) {
27+
break;
28+
}
29+
30+
let mut b = blocks.clone();
31+
b.push(block.clone());
32+
helper(b, digits, j + 1, ans);
33+
}
34+
}
35+
}
36+
37+
let mut ans: Vec<String> = vec![];
38+
helper(vec![], &digits, 0, &mut ans);
39+
ans
40+
}
41+
}
42+
43+
struct Solution {}

‎src/leetcode/problem/rotate_the_box.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// 1861. Rotating the Box, Medium
2+
// https://leetcode.com/problems/rotating-the-box/
3+
impl Solution {
4+
pub fn rotate_the_box(grid: Vec<Vec<char>>) -> Vec<Vec<char>> {
5+
let [n, m] = [grid.len(), grid[0].len()];
6+
7+
// gravity -> move everything to the right
8+
let mut grid = grid;
9+
for i in 0..n {
10+
for j in (0..m).rev() {
11+
if grid[i][j] == '.' {
12+
let mut k = j;
13+
let mut stone_k = -1;
14+
while k > 0 && grid[i][k] != '*' {
15+
k -= 1;
16+
if grid[i][k] == '#' {
17+
stone_k = k as i32;
18+
}
19+
}
20+
if stone_k != -1 {
21+
grid[i][stone_k as usize] = '.';
22+
grid[i][j] = '#';
23+
}
24+
}
25+
}
26+
}
27+
28+
let mut res = vec![vec!['.'; n]; m];
29+
30+
for i in 0..n {
31+
for j in 0..m {
32+
res[j][n - i - 1] = grid[i][j];
33+
}
34+
}
35+
36+
res
37+
}
38+
}
39+
40+
struct Solution {}

‎src/main.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,20 @@ fn gen_table() -> Result<()> {
5353
}
5454

5555
entries.sort();
56+
57+
entries.iter_mut().for_each(|f| match f.2.as_str() {
58+
"Easy" => {
59+
f.2 = "![easy](https://shields.io/badge/-easy-green)".to_string();
60+
}
61+
"Medium" => {
62+
f.2 = "![medium](https://shields.io/badge/-medium-yellow)".to_string();
63+
}
64+
"Hard" => {
65+
f.2 = "![hard](https://shields.io/badge/-hard-red)".to_string();
66+
}
67+
_ => {}
68+
});
69+
5670
println!("## Solutions ({}) ", entries.len());
5771
println!(
5872
r#"| No. | Title | Solution | Problem | Difficulty |

0 commit comments

Comments
(0)

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