Skip to content

Commit 3b245b4

Browse files
committed
Add 5 solutions
1 parent 7412fad commit 3b245b4

File tree

9 files changed

+256
-1
lines changed

9 files changed

+256
-1
lines changed

src/leetcode/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod challenge;
22
mod problem;
3+
mod top_interview;
34
#[cfg_attr(test, macro_use)]
45
mod util;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use std::collections::VecDeque;
2+
3+
// 55. Jump Game, Medium
4+
// https://leetcode.com/problems/jump-game/
5+
impl Solution {
6+
pub fn can_jump(nums: Vec<i32>) -> bool {
7+
let mut pos: VecDeque<usize> = VecDeque::new();
8+
pos.push_back(0);
9+
let mut jumps = nums.clone();
10+
11+
while pos.len() > 0 && *pos.back().unwrap() < nums.len() - 1 {
12+
let mut p = *pos.back().unwrap();
13+
if jumps[p] == 0 && pos.len() > 1 {
14+
pos.pop_back();
15+
p = *pos.back().unwrap();
16+
jumps[p] -= 1;
17+
} else if jumps[p] == 0 && pos.len() <= 1 {
18+
return false;
19+
} else {
20+
p += jumps[p] as usize;
21+
pos.push_back(p);
22+
}
23+
}
24+
25+
return pos.len() > 0 && *pos.back().unwrap() >= nums.len() - 1;
26+
}
27+
}
28+
29+
struct Solution {}
30+
31+
#[cfg(test)]
32+
mod tests {
33+
use super::*;
34+
use crate::{vec_string, vec_vec_i32, vec_vec_string};
35+
36+
#[test]
37+
fn test_can_jump() {
38+
assert_eq!(Solution::can_jump(vec![2, 3, 1, 1, 4]), true);
39+
}
40+
41+
#[test]
42+
fn test_can_jump2() {
43+
assert_eq!(Solution::can_jump(vec![3, 2, 1, 0, 4]), false);
44+
}
45+
46+
#[test]
47+
fn test_can_jump3() {
48+
assert_eq!(Solution::can_jump(vec![3, 0, 8, 2, 0, 0, 1]), true);
49+
}
50+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use std::collections::HashMap;
2+
3+
// 49. Group Anagrams, Medium
4+
// https://leetcode.com/problems/group-anagrams/
5+
impl Solution {
6+
pub fn group_anagrams(strs: Vec<String>) -> Vec<Vec<String>> {
7+
let mut ans: Vec<Vec<String>> = Vec::new();
8+
let mut map: HashMap<String, Vec<String>> = HashMap::new();
9+
10+
for str in strs {
11+
let mut chars = str.chars().collect::<Vec<char>>();
12+
chars.sort();
13+
let key: String = chars.into_iter().collect();
14+
if let Some(v) = map.get_mut(&key) {
15+
v.push(str);
16+
} else {
17+
map.insert(key, vec![str]);
18+
}
19+
}
20+
21+
for (_, v) in map {
22+
ans.push(v);
23+
}
24+
return ans;
25+
}
26+
}
27+
28+
struct Solution {}
29+
30+
#[cfg(test)]
31+
mod tests {
32+
use super::*;
33+
use crate::{vec_string, vec_vec_i32, vec_vec_string};
34+
35+
#[test]
36+
fn test_group_anagrams() {
37+
assert_eq!(Solution::group_anagrams(vec_string!["eat", "tea", "tan", "ate", "nat", "bat"]).len(), 3);
38+
}
39+
40+
#[test]
41+
fn test_group_anagrams2() {
42+
assert_eq!(Solution::group_anagrams(vec_string![""]), vec_vec_string![[""]]);
43+
}
44+
45+
#[test]
46+
fn test_group_anagrams3() {
47+
assert_eq!(Solution::group_anagrams(vec_string!["a"]), vec_vec_string![["a"]]);
48+
}
49+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use std::collections::VecDeque;
2+
3+
// 334. Increasing Triplet Subsequence, Medium
4+
// https://leetcode.com/problems/increasing-triplet-subsequence/
5+
impl Solution {
6+
pub fn increasing_triplet(nums: Vec<i32>) -> bool {
7+
let [mut small, mut big] = [std::i32::MAX, std::i32::MAX];
8+
9+
for num in nums {
10+
if num <= small {
11+
small = num;
12+
} else if num <= big {
13+
big = num;
14+
} else {
15+
return true;
16+
}
17+
}
18+
19+
return false;
20+
}
21+
}
22+
23+
struct Solution {}
24+
25+
#[cfg(test)]
26+
mod tests {
27+
use super::*;
28+
use crate::{vec_string, vec_vec_i32, vec_vec_string};
29+
30+
#[test]
31+
fn test_increasing_triplet() {
32+
assert_eq!(Solution::increasing_triplet(vec![1, 2, 3, 4, 5]), true);
33+
}
34+
35+
#[test]
36+
fn test_increasing_triplet2() {
37+
assert_eq!(Solution::increasing_triplet(vec![5, 4, 3, 2, 1]), false);
38+
}
39+
40+
#[test]
41+
fn test_increasing_triplet3() {
42+
assert_eq!(Solution::increasing_triplet(vec![2, 1, 5, 0, 4, 6]), true);
43+
}
44+
45+
#[test]
46+
fn test_increasing_triplet4() {
47+
assert_eq!(Solution::increasing_triplet(vec![20, 100, 10, 12, 5, 13]), true);
48+
}
49+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// 3. Longest Substring Without Repeating Characters, Medium
2+
// https://leetcode.com/problems/longest-substring-without-repeating-characters/
3+
impl Solution {
4+
pub fn length_of_longest_substring(s: String) -> i32 {
5+
let mut max = 0;
6+
7+
let mut q = Vec::new();
8+
for ch in s.chars() {
9+
while q.contains(&ch) {
10+
q.remove(0);
11+
}
12+
q.push(ch);
13+
max = max.max(q.len() as i32);
14+
}
15+
16+
max
17+
}
18+
}
19+
20+
struct Solution {}
21+
22+
#[cfg(test)]
23+
mod tests {
24+
use super::*;
25+
use crate::vec_vec_i32;
26+
27+
#[test]
28+
fn test_length_of_longest_substring() {
29+
assert_eq!(Solution::length_of_longest_substring("abcabcbb".to_string()), 3);
30+
}
31+
32+
#[test]
33+
fn test_length_of_longest_substring2() {
34+
assert_eq!(Solution::length_of_longest_substring("bbbbb".to_string()), 1);
35+
}
36+
37+
#[test]
38+
fn test_length_of_longest_substring3() {
39+
assert_eq!(Solution::length_of_longest_substring("pwwkew".to_string()), 3);
40+
}
41+
42+
#[test]
43+
fn test_length_of_longest_substring4() {
44+
assert_eq!(Solution::length_of_longest_substring("".to_string()), 0);
45+
}
46+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
mod can_jump;
2+
mod group_anagrams;
3+
mod increasing_triplet;
4+
mod length_of_longest_substring;
5+
mod set_zeroes;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// 73. Set Matrix Zeroes, Medium
2+
// https://leetcode.com/problems/set-matrix-zeroes/submissions/
3+
impl Solution {
4+
pub fn set_zeroes(matrix: &mut Vec<Vec<i32>>) {
5+
let [n, m] = [matrix.len(), matrix[0].len()];
6+
let mut zeros: Vec<(usize, usize)> = Vec::new();
7+
8+
for i in 0..n {
9+
for j in 0..m {
10+
if matrix[i][j] == 0 {
11+
zeros.push((i, j));
12+
}
13+
}
14+
}
15+
16+
for (zi, zj) in zeros {
17+
for i in 0..n {
18+
matrix[i][zj] = 0;
19+
}
20+
for j in 0..m {
21+
matrix[zi][j] = 0;
22+
}
23+
}
24+
}
25+
}
26+
27+
struct Solution {}
28+
29+
#[cfg(test)]
30+
mod tests {
31+
use super::*;
32+
use crate::vec_vec_i32;
33+
34+
#[test]
35+
fn test_set_zeroes() {
36+
let mut matrix = vec_vec_i32![[1, 1, 1], [1, 0, 1], [1, 1, 1]];
37+
Solution::set_zeroes(&mut matrix);
38+
assert_eq!(matrix, vec_vec_i32![[1, 0, 1], [0, 0, 0], [1, 0, 1]]);
39+
}
40+
41+
#[test]
42+
fn test_set_zeroes2() {
43+
let mut matrix = vec_vec_i32![[0, 1, 2, 0], [3, 4, 5, 2], [1, 3, 1, 5]];
44+
Solution::set_zeroes(&mut matrix);
45+
assert_eq!(matrix, vec_vec_i32![[0, 0, 0, 0], [0, 4, 5, 0], [0, 3, 1, 0]]);
46+
}
47+
}

src/leetcode/top_interview/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mod array_and_string;

src/leetcode/util/vec_vec.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ macro_rules! vec_vec_i32 {
55
};
66
}
77

8+
#[macro_export]
9+
macro_rules! vec_string {
10+
($($tail:tt),*) => {
11+
vec![$($tail.to_string()),*] as Vec<String>
12+
};
13+
}
14+
815
#[macro_export]
916
macro_rules! vec_vec_string {
1017
($($tail:tt),*) => {
@@ -17,4 +24,4 @@ macro_rules! vec_vec_char {
1724
($($tail:tt),*) => {
1825
vec![$(vec!$tail),*] as Vec<Vec<char>>
1926
};
20-
}
27+
}

0 commit comments

Comments
 (0)