Skip to content

Commit 9fc3ce9

Browse files
committed
Add more solutions
1 parent 2455bcb commit 9fc3ce9

File tree

9 files changed

+113
-2
lines changed

9 files changed

+113
-2
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ LeetCode is a website that has programming-related questions that are designed t
1313
| 3 | Longest Substring Without Repeating Characters | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/top_interview/array_and_string/length_of_longest_substring.rs) | [Leetcode](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | Medium |
1414
| 49 | Group Anagrams | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/top_interview/array_and_string/group_anagrams.rs) | [Leetcode](https://leetcode.com/problems/group-anagrams/) | Medium |
1515
| 54 | Spiral Matrix | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/spiral_order.rs) | [Leetcode](https://leetcode.com/problems/spiral-matrix/) | Medium |
16-
| 55 | Jump Game | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/top_interview/array_and_string/can_jump.rs) | [Leetcode](https://leetcode.com/problems/jump-game/) | Medium |
16+
| 55 | Jump Game | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/top_interview/dynamic_programming/can_jump.rs) | [Leetcode](https://leetcode.com/problems/jump-game/) | Medium |
1717
| 72 | Edit Distance | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/min_distance.rs) | [Leetcode](https://leetcode.com/problems/edit-distance/) | Hard |
1818
| 73 | Set Matrix Zeroes | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/top_interview/array_and_string/set_zeroes.rs) | [Leetcode](https://leetcode.com/problems/set-matrix-zeroes/submissions/) | Medium |
19+
| 75 | Sort Colors | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/top_interview/sorting/sort_colors.rs) | [Leetcode](https://leetcode.com/problems/sort-colors/) | Medium |
1920
| 115 | Distinct Subsequences | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/num_distinct.rs) | [Leetcode](https://leetcode.com/problems/distinct-subsequences/) | Hard |
21+
| 169 | Majority Element | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/top_interview/others/majority_element.rs) | [Leetcode](https://leetcode.com/problems/majority-element/solution/) | Easy |
2022
| 242 | Valid Anagram | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/is_anagram.rs) | [Leetcode](https://leetcode.com/problems/valid-anagram/) | Easy |
2123
| 282 | Expression Add Operators | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/add_operators.rs) | [Leetcode](https://leetcode.com/problems/expression-add-operators/) | Hard |
2224
| 317 | Shortest Distance from All Buildings | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/shortest_distance.rs) | [Leetcode](https://leetcode.com/problems/shortest-distance-from-all-buildings/) | Hard |

src/leetcode/top_interview/array_and_string/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
mod can_jump;
21
mod group_anagrams;
32
mod increasing_triplet;
43
mod length_of_longest_substring;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mod can_jump;

src/leetcode/top_interview/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
mod array_and_string;
2+
mod dynamic_programming;
3+
mod others;
4+
mod sorting;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use std::collections::VecDeque;
2+
3+
// 169. Majority Element, Easy
4+
// https://leetcode.com/problems/majority-element/solution/
5+
impl Solution {
6+
pub fn majority_element(nums: Vec<i32>) -> i32 {
7+
let mut count = 0;
8+
let mut cand: Option<i32> = None;
9+
10+
for num in nums {
11+
if cand.is_none() || count == 0 {
12+
cand = Some(num);
13+
count = 1;
14+
} else if cand.unwrap() == num {
15+
count += 1;
16+
} else if count > 0 {
17+
count -= 1;
18+
}
19+
}
20+
21+
return cand.unwrap();
22+
}
23+
}
24+
25+
struct Solution {}
26+
27+
#[cfg(test)]
28+
mod tests {
29+
use super::*;
30+
31+
#[test]
32+
fn test_majority_element() {
33+
assert_eq!(Solution::majority_element(vec![3, 2, 3]), 3);
34+
}
35+
36+
#[test]
37+
fn test_majority_element2() {
38+
assert_eq!(Solution::majority_element(vec![2, 2, 1, 1, 1, 2, 2]), 2);
39+
}
40+
41+
#[test]
42+
fn test_majority_element3() {
43+
assert_eq!(Solution::majority_element(vec![3, 3, 4]), 3);
44+
}
45+
}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mod majority_element;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mod sort_colors;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// 75. Sort Colors, Medium
2+
// https://leetcode.com/problems/sort-colors/
3+
impl Solution {
4+
pub fn sort_colors(nums: &mut Vec<i32>) {
5+
let n = nums.len();
6+
if n <= 1 {
7+
return;
8+
}
9+
10+
let mut swapped = true;
11+
12+
while swapped {
13+
swapped = false;
14+
for i in 0..n - 1 {
15+
if nums[i] > nums[i + 1] {
16+
nums.swap(i, i + 1);
17+
swapped = true;
18+
break;
19+
}
20+
}
21+
}
22+
}
23+
}
24+
25+
struct Solution {}
26+
27+
#[cfg(test)]
28+
mod tests {
29+
use super::*;
30+
use crate::{vec_string, vec_vec_i32, vec_vec_string};
31+
32+
#[test]
33+
fn test_sort_colors() {
34+
let mut colors = vec![2, 0, 2, 1, 1, 0];
35+
Solution::sort_colors(&mut colors);
36+
assert_eq!(colors, vec![0, 0, 1, 1, 2, 2]);
37+
}
38+
39+
#[test]
40+
fn test_sort_colors2() {
41+
let mut colors = vec![2, 0, 1];
42+
Solution::sort_colors(&mut colors);
43+
assert_eq!(colors, vec![0, 1, 2]);
44+
}
45+
46+
#[test]
47+
fn test_sort_colors3() {
48+
let mut colors = vec![0];
49+
Solution::sort_colors(&mut colors);
50+
assert_eq!(colors, vec![0]);
51+
}
52+
53+
#[test]
54+
fn test_sort_colors4() {
55+
let mut colors = vec![1];
56+
Solution::sort_colors(&mut colors);
57+
assert_eq!(colors, vec![1]);
58+
}
59+
}

0 commit comments

Comments
 (0)