Skip to content

Commit ceed131

Browse files
committed
12/7/2020
1 parent 3408c62 commit ceed131

7 files changed

+169
-31
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@ leetcode practise solution in rust
2424
|46|[Permutations](src/_0046_permutations.rs)|
2525
|49|[Group Anagrams](src/_0049_group_anagrams.rs)|
2626
|53|[Maximum Subarray](src/_0053_maximum_subarray.rs)|
27+
|54|[Spiral Matrix](src/_0054_spiral_matrix.rs)|
2728
|56|[Merge Intervals](src/_0056_merge_intervals.rs)|
2829
|66|[Plus One](src/_0066_plus_one.rs)|
2930
|67|[Add Binary](src/_0067_add_binary.rs)|
3031
|70|[Climbing Stairs](src/_0070_climbing_stairs.rs)|
3132
|74|[Search a 2D Matrix](src/_0074_search_a_2d_matrix.rs)|
3233
|78|[Subsets](src/_0078_subsets.rs)|
33-
|79|[Word Search](src/_0079_word_search)|
34+
|79|[Word Search](src/_0079_word_search.rs)|
3435
|83|[Remove Duplicates from Sorted List](src/_0083_remove_duplicates_from_sorted_list.rs)|
3536
|88|[Merge Sorted Array](src/_0088_merge_sorted_array.rs)|
3637
|91|[Decode Ways](src/_0091_decode_ways.rs)|
@@ -43,6 +44,7 @@ leetcode practise solution in rust
4344
|125|[Valid Palindrome](src/_0125_valid_palindrome.rs)|
4445
|127|[Word Ladder](src/_0127_word_ladder.rs)|
4546
|136|[Single Number](src/_0136_single_number.rs)|
47+
|151|[Reverse Words in a String](src/_0151_reverse_words_in_a_string.rs)|
4648
|155|[Min Stack](src/_0155_min_stack.rs)|
4749
|167|[Two Sum II - Input array is sorted](src/_0167_two_sum_ii.rs)|
4850
|169|[Majority Element](src/_0169_majority_element.rs)|

src/_0054_spiral_matrix.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn spiral_order(matrix: Vec<Vec<i32>>) -> Vec<i32> {
5+
let direction: Vec<(i32, i32)> = vec![(0, 1), (1, 0), (0, -1), (-1, 0)];
6+
let mut result = vec![];
7+
8+
let mut cur_pos = (0, 0);
9+
let mut margin: (i32, i32, i32, i32) =
10+
(0, matrix.len() as i32 - 1, 0, matrix[0].len() as i32 - 1); // up, bottom, left, right
11+
let mut cur_dir: usize = 0;
12+
let mut visited = 0;
13+
14+
while visited < matrix[0].len() * matrix.len() {
15+
result.push(matrix[cur_pos.0][cur_pos.1]);
16+
visited += 1;
17+
18+
let next_pos: (i32, i32) = (
19+
cur_pos.0 as i32 + direction[cur_dir].0,
20+
cur_pos.1 as i32 + direction[cur_dir].1,
21+
);
22+
if (next_pos.0 < margin.0 || next_pos.0 > margin.1)
23+
|| (next_pos.1 < margin.2 || next_pos.1 > margin.3)
24+
{
25+
cur_dir = if cur_dir == 3 { 0 } else { cur_dir + 1 };
26+
match cur_dir {
27+
0 => margin.2 += 1,
28+
1 => margin.0 += 1,
29+
2 => margin.3 -= 1,
30+
3 => margin.1 -= 1,
31+
_ => {}
32+
}
33+
}
34+
cur_pos = (
35+
(cur_pos.0 as i32 + direction[cur_dir].0) as usize,
36+
(cur_pos.1 as i32 + direction[cur_dir].1) as usize,
37+
);
38+
}
39+
40+
result
41+
}
42+
}
43+
44+
#[test]
45+
fn test() {
46+
let matrix: Vec<Vec<i32>> = vec_vec_i32![[1, 2, 3], [4, 5, 6], [7, 8, 9]];
47+
let res = vec![1, 2, 3, 6, 9, 8, 7, 4, 5];
48+
assert_eq!(Solution::spiral_order(matrix), res);
49+
let matrix: Vec<Vec<i32>> = vec_vec_i32![[3], [2]];
50+
let res = vec![3, 2];
51+
assert_eq!(Solution::spiral_order(matrix), res);
52+
}

src/_0112_path_sum.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use crate::util::*;
2+
use std::cell::RefCell;
3+
use std::rc::Rc;
4+
5+
struct Solution;
6+
impl Solution {
7+
pub fn has_path_sum(root: Option<Rc<RefCell<TreeNode>>>, sum: i32) -> bool {
8+
if root.is_some() {
9+
Self::dfs(&root, sum)
10+
} else {
11+
false
12+
}
13+
}
14+
15+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, sum: i32) -> bool {
16+
if let Some(root) = root {
17+
let root = root.borrow();
18+
match (&root.left, &root.right) {
19+
(None, None) => sum == root.val,
20+
(left, right) => {
21+
if (left.is_some() && Self::dfs(left, sum - root.val))
22+
|| (right.is_some() && Self::dfs(right, sum - root.val))
23+
{
24+
return true;
25+
} else {
26+
return false;
27+
}
28+
}
29+
}
30+
} else {
31+
sum == 0
32+
}
33+
}
34+
}
35+
36+
#[test]
37+
fn test() {
38+
let root = tree!(
39+
5,
40+
tree!(4, tree!(11, tree!(7), tree!(2)), None),
41+
tree!(8, tree!(13), tree!(4, None, tree!(1)))
42+
);
43+
assert_eq!(Solution::has_path_sum(root, 22), true);
44+
assert_eq!(Solution::has_path_sum(tree!(1, tree!(2), None), 1), false);
45+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
struct Solution;
2+
impl Solution {
3+
pub fn reverse_words(s: String) -> String {
4+
s.trim();
5+
let mut words: Vec<&str> = s.split_whitespace().collect();
6+
words.reverse();
7+
let result = words.join(" ");
8+
result
9+
}
10+
}
11+
12+
#[test]
13+
fn test() {
14+
assert_eq!(
15+
Solution::reverse_words("the sky is blue".to_string()),
16+
"blue is sky the".to_string()
17+
);
18+
assert_eq!(
19+
Solution::reverse_words(" hello world ".to_string()),
20+
"world hello".to_string()
21+
);
22+
assert_eq!(
23+
Solution::reverse_words("a good example".to_string()),
24+
"example good a".to_string()
25+
);
26+
assert_eq!(
27+
Solution::reverse_words(" Bob Loves Alice ".to_string()),
28+
"Alice Loves Bob".to_string()
29+
);
30+
assert_eq!(
31+
Solution::reverse_words("Alice does not even like bob".to_string()),
32+
"bob like even not does Alice".to_string()
33+
);
34+
}

src/_1679_max_number_of_k-sum_pairs.rs renamed to src/_1679_max_number_of_ksum_pairs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ impl Solution {
2222

2323
#[test]
2424
fn test() {
25-
assert_eq!(Solution::max_operations(vec![1, 2, 3, 4], 2), 5);
25+
assert_eq!(Solution::max_operations(vec![1, 2, 3, 4], 5), 2);
2626
assert_eq!(Solution::max_operations(vec![3, 1, 3, 4, 3], 6), 1);
2727
}
Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
struct Solution;
1+
// struct Solution;
22

3-
impl Solution {
4-
pub fn concatenated_binary(n: i32) -> i32 {
5-
let mut result: usize = 0;
6-
for i in 1..=n as usize {
7-
result <<= Self::binary_digits(i);
8-
result += i;
9-
result %= 1_000_000_007;
10-
}
11-
result as i32
12-
}
3+
// impl Solution {
4+
// pub fn concatenated_binary(n: i32) -> i32 {
5+
// let mut result: usize = 0;
6+
// for i in 1..=n as usize {
7+
// result <<= Self::binary_digits(i);
8+
// result += i;
9+
// result %= 1_000_000_007;
10+
// }
11+
// result as i32
12+
// }
1313

14-
fn binary_digits(mut decimal: usize) -> usize {
15-
let mut bits = 0;
16-
while decimal > 0 {
17-
bits += 1;
18-
decimal >> 1;
19-
}
20-
bits
21-
}
22-
}
14+
// fn binary_digits(mut decimal: usize) -> usize {
15+
// let mut bits = 0;
16+
// while decimal > 0 {
17+
// bits += 1;
18+
// decimal >> 1;
19+
// }
20+
// bits
21+
// }
22+
// }
2323

24-
#[test]
25-
fn test() {
26-
assert_eq!(Solution::concatenated_binary(1), 1);
27-
assert_eq!(Solution::concatenated_binary(3), 3);
28-
assert_eq!(Solution::concatenated_binary(12), 505379714);
29-
}
24+
// #[test]
25+
// fn test() {
26+
// assert_eq!(Solution::concatenated_binary(1), 1);
27+
// assert_eq!(Solution::concatenated_binary(3), 3);
28+
// assert_eq!(Solution::concatenated_binary(12), 505379714);
29+
// }

src/lib.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ mod _0049_group_anagrams;
4848
//
4949
mod _0053_maximum_subarray;
5050
//
51+
mod _0054_spiral_matrix;
52+
//
5153
mod _0056_merge_intervals;
5254
//
5355
mod _0066_plus_one;
@@ -76,6 +78,8 @@ mod _0104_maximum_depth_of_binary_tree;
7678
//
7779
mod _0110_balanced_binary_tree;
7880
//
81+
mod _0112_path_sum;
82+
//
7983
mod _0121_best_time_to_buy_and_sell_stock;
8084
//
8185
mod _0122_best_time_to_buy_and_sell_stock_ii;
@@ -86,6 +90,8 @@ mod _0127_word_ladder;
8690
//
8791
mod _0136_single_number;
8892
//
93+
mod _0151_reverse_words_in_a_string;
94+
//
8995
mod _0155_min_stack;
9096
//
9197
mod _0167_two_sum_ii;
@@ -312,7 +318,6 @@ mod _1608_special_array_with_x_elements_greater_than_or_equal_x;
312318
//
313319
mod _1609_even_odd_tree;
314320
//
315-
mod test;
316-
321+
mod _1679_max_number_of_ksum_pairs;
317322
//
318-
mod temp1;
323+
mod _1680_concatenation_of_consecutive_binary_numbers;

0 commit comments

Comments
 (0)