Skip to content

Commit 35f10b4

Browse files
committed
Add more solutions
1 parent f8a7d65 commit 35f10b4

9 files changed

+397
-30
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ version = "0.1.0"
44
edition = "2018"
55

66
[dependencies]
7+
automod = "1.0.2"
78
regex = "1.5.4"
89
walkdir = "2.3.2"

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@
66

77
LeetCode is a website that has programming-related questions that are designed to be solved in a limited amount of time. This repository is a collection of some of my solutions written in [Rust](https://www.rust-lang.org/).
88

9-
## Solutions (62)
9+
## Solutions (67)
1010
| No. | Title | Solution | Problem | Difficulty |
1111
|:---:|:------|:--------:|:-------:|:----------:|
1212
| 1 | Two Sum | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/two_sum.rs) | [Leetcode](https://leetcode.com/problems/two-sum/) | Easy |
1313
| 2 | Add Two Numbers | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/top_interview/linked_list/add_two_numbers.rs) | [Leetcode](https://leetcode.com/problems/add-two-numbers/) | Medium |
1414
| 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 |
1515
| 5 | Longest Palindromic Substring | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/top_interview/array_and_string/longest_palindrome.rs) | [Leetcode](https://leetcode.com/problems/longest-palindromic-substring/) | Medium |
16+
| 31 | Next Permutation | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/next_permutation.rs) | [Leetcode](https://leetcode.com/problems/next-permutation/) | Medium |
17+
| 37 | Sudoku Solver | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/solve_sudoku.rs) | [Leetcode](https://leetcode.com/problems/sudoku-solver/) | Hard |
1618
| 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 |
1719
| 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 |
1820
| 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 |
1921
| 62 | Unique Paths | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/unique_paths.rs) | [Leetcode](https://leetcode.com/problems/unique-paths/) | Medium |
22+
| 70 | Climbing Stairs | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/climb_stairs.rs) | [Leetcode](https://leetcode.com/problems/climbing-stairs/) | Easy |
2023
| 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 |
2124
| 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 |
2225
| 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 |
@@ -38,6 +41,7 @@ LeetCode is a website that has programming-related questions that are designed t
3841
| 350 | Intersection of Two Arrays II | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/intersect.rs) | [Leetcode](https://leetcode.com/problems/intersection-of-two-arrays-ii/) | Easy |
3942
| 368 | Largest Divisible Subset | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/largest_divisible_subset.rs) | [Leetcode](https://leetcode.com/problems/largest-divisible-subset/) | Medium |
4043
| 435 | Non-overlapping Intervals | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/erase_overlap_intervals.rs) | [Leetcode](https://leetcode.com/problems/non-overlapping-intervals/) | Medium |
44+
| 442 | Find All Duplicates in an Array | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/find_duplicates.rs) | [Leetcode](https://leetcode.com/problems/find-all-duplicates-in-an-array/) | Medium |
4145
| 452 | Minimum Number of Arrows to Burst Balloons | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/find_min_arrow_shots.rs) | [Leetcode](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/) | Medium |
4246
| 463 | Island Perimeter | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/island_perimeter.rs) | [Leetcode](https://leetcode.com/problems/island-perimeter/) | Easy |
4347
| 485 | Max Consecutive Ones | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/find_max_consecutive_ones.rs) | [Leetcode](https://leetcode.com/problems/max-consecutive-ones/) | Easy |
@@ -62,6 +66,7 @@ LeetCode is a website that has programming-related questions that are designed t
6266
| 1293 | Shortest Path in a Grid with Obstacles Elimination | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/shortest_path.rs) | [Leetcode](https://leetcode.com/problems/shortest-path-in-a-grid-with-obstacles-elimination/) | Hard |
6367
| 1315 | Sum of Nodes with Even-Valued Grandparent | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/sum_even_grandparent.rs) | [Leetcode](https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/) | Medium |
6468
| 1328 | Break a Palindrome | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/break_palindrome.rs) | [Leetcode](https://leetcode.com/problems/break-a-palindrome/) | Medium |
69+
| 1428 | Leftmost Column with at Least a One | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/left_most_column_with_one.rs) | [Leetcode](https://leetcode.com/problems/leftmost-column-with-at-least-a-one/) | Medium |
6570
| 1457 | Pseudo-Palindromic Paths in a Binary Tree | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/pseudo_palindromic_paths.rs) | [Leetcode](https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/) | Easy |
6671
| 1801 | Number of Orders in the Backlog | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/get_number_of_backlog_orders.rs) | [Leetcode](https://leetcode.com/problems/number-of-orders-in-the-backlog/) | Medium |
6772
| 1962 | Remove Stones to Minimize the Total | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/min_stone_sum.rs) | [Leetcode](https://leetcode.com/problems/remove-stones-to-minimize-the-total/) | Medium |
@@ -70,4 +75,4 @@ LeetCode is a website that has programming-related questions that are designed t
7075
| 2024 | Maximize the Confusion of an Exam | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/contest/biweekly_62.rs) | [Leetcode](https://leetcode.com/problems/maximize-the-confusion-of-an-exam/) | Medium |
7176
| 2025 | Maximum Number of Ways to Partition an Array | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/contest/biweekly_62.rs) | [Leetcode](https://leetcode.com/problems/maximum-number-of-ways-to-partition-an-array/) | Hard |
7277
| 2027 | Minimum Moves to Convert String | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/contest/weekly_261.rs) | [Leetcode](https://leetcode.com/problems/minimum-moves-to-convert-string/) | Easy |
73-
| 2028 | Find Missing Observations | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/contest/weekly_261.rs) | [Leetcode](https://leetcode.com/problems/find-missing-observations/) | Medium |
78+
| 2028 | Find Missing Observations | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/contest/weekly_261.rs) | [Leetcode](https://leetcode.com/problems/find-missing-observations/) | Medium |

src/leetcode/problem/climb_stairs.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// 70. Climbing Stairs, Easy
2+
// https://leetcode.com/problems/climbing-stairs/
3+
impl Solution {
4+
pub fn climb_stairs(n: i32) -> i32 {
5+
let mut dp = vec![0; n.max(4) as usize + 1];
6+
dp[0] = 0;
7+
dp[1] = 1;
8+
dp[2] = 2;
9+
dp[3] = 3;
10+
for i in 4..=n as usize {
11+
dp[i] = dp[i - 1] + dp[i - 2];
12+
}
13+
return dp[n as usize];
14+
}
15+
}
16+
17+
struct Solution {}
18+
19+
#[cfg(test)]
20+
mod tests {
21+
use super::*;
22+
23+
#[test]
24+
fn test_climb_stairs() {
25+
assert_eq!(Solution::climb_stairs(2), 2);
26+
}
27+
28+
#[test]
29+
fn test_climb_stairs2() {
30+
assert_eq!(Solution::climb_stairs(3), 3);
31+
}
32+
33+
#[test]
34+
fn test_climb_stairs3() {
35+
assert_eq!(Solution::climb_stairs(4), 5);
36+
}
37+
}
38+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// 442. Find All Duplicates in an Array, Medium
2+
// https://leetcode.com/problems/find-all-duplicates-in-an-array/
3+
impl Solution {
4+
pub fn find_duplicates(mut nums: Vec<i32>) -> Vec<i32> {
5+
nums.sort_unstable();
6+
7+
let mut ans = vec![];
8+
let n = nums.len();
9+
if n == 0 {
10+
return ans;
11+
}
12+
13+
let mut prev = nums[0];
14+
15+
for i in 1..n {
16+
if nums[i] == prev {
17+
ans.push(nums[i]);
18+
}
19+
prev = nums[i];
20+
}
21+
22+
ans
23+
}
24+
}
25+
26+
struct Solution {}
27+
28+
#[cfg(test)]
29+
mod tests {
30+
use super::*;
31+
32+
#[test]
33+
fn test_find_duplicates() {
34+
assert_eq!(Solution::find_duplicates(vec![4, 3, 2, 7, 8, 2, 3, 1]), vec![2, 3]);
35+
}
36+
37+
#[test]
38+
fn test_find_duplicates2() {
39+
assert_eq!(Solution::find_duplicates(vec![1, 1, 2]), vec![1]);
40+
}
41+
42+
#[test]
43+
fn test_find_duplicates3() {
44+
assert_eq!(Solution::find_duplicates(vec![1]), vec![]);
45+
}
46+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#[derive(Copy, Clone)]
2+
struct BinaryMatrix;
3+
4+
#[allow(unused_variables)]
5+
impl BinaryMatrix {
6+
fn get(self, row: i32, col: i32) -> i32 {
7+
1
8+
}
9+
10+
fn dimensions(self) -> Vec<i32> {
11+
vec![]
12+
}
13+
}
14+
15+
// 1428. Leftmost Column with at Least a One, Medium
16+
// https://leetcode.com/problems/leftmost-column-with-at-least-a-one/
17+
impl Solution {
18+
pub fn left_most_column_with_one(binary_matrix: &BinaryMatrix) -> i32 {
19+
let dim: Vec<_> = binary_matrix.dimensions();
20+
21+
let mut row = dim[0] - 1;
22+
let mut col = dim[1] - 1;
23+
24+
let mut candidate: Option<i32> = None;
25+
26+
while row >= 0 && col >= 0 {
27+
if binary_matrix.get(row, col) == 1 {
28+
candidate = Some(col);
29+
col -= 1;
30+
} else {
31+
row -= 1;
32+
}
33+
}
34+
35+
candidate.unwrap_or(-1)
36+
}
37+
}
38+
39+
struct Solution {}

src/leetcode/problem/mod.rs

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1 @@
1-
mod add_operators;
2-
mod broken_calc;
3-
mod can_complete_circuit;
4-
mod coin_change;
5-
mod contain_virus;
6-
mod erase_overlap_intervals;
7-
mod find_min_arrow_shots;
8-
mod find_poisoned_duration;
9-
mod get_number_of_backlog_orders;
10-
mod inorder_traversal;
11-
mod is_anagram;
12-
mod is_good_array;
13-
mod is_same_tree;
14-
mod island_perimeter;
15-
mod kth_smallest;
16-
mod largest_divisible_subset;
17-
mod max_level_sum;
18-
mod max_uncrossed_lines;
19-
mod min_distance;
20-
mod min_eating_speed;
21-
mod min_stone_sum;
22-
mod num_islands;
23-
mod num_subarray_product_less_than_k;
24-
mod pseudo_palindromic_paths;
25-
mod sum_even_grandparent;
26-
mod two_sum;
27-
mod unique_paths;
28-
mod zigzag_level_order;
1+
automod::dir!("src/leetcode/problem");
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// 31. Next Permutation, Medium
2+
// https://leetcode.com/problems/next-permutation/
3+
impl Solution {
4+
pub fn next_permutation(nums: &mut Vec<i32>) {
5+
let n = nums.len();
6+
7+
if n <= 1 {
8+
return;
9+
}
10+
11+
let mut swapped_at = 0;
12+
'outer: for l in (0..n).rev() {
13+
for r in (0..n).rev() {
14+
if r > l && nums[r] > nums[l] {
15+
nums.swap(l, r);
16+
swapped_at = l + 1;
17+
break 'outer;
18+
}
19+
}
20+
}
21+
22+
let mut i = swapped_at;
23+
while i < n - 1 {
24+
if nums[i] > nums[i + 1] {
25+
nums.swap(i, i + 1);
26+
i = swapped_at;
27+
continue;
28+
}
29+
i += 1;
30+
}
31+
}
32+
}
33+
34+
struct Solution {}
35+
36+
#[cfg(test)]
37+
mod tests {
38+
use super::*;
39+
use crate::vec_vec_i32;
40+
41+
#[test]
42+
fn test_next_permutation() {
43+
let mut nums = vec![1, 2, 3];
44+
Solution::next_permutation(&mut nums);
45+
assert_eq!(nums, vec![1, 3, 2]);
46+
}
47+
48+
#[test]
49+
fn test_next_permutation2() {
50+
let mut nums = vec![3, 2, 1];
51+
Solution::next_permutation(&mut nums);
52+
assert_eq!(nums, vec![1, 2, 3]);
53+
}
54+
55+
#[test]
56+
fn test_next_permutation3() {
57+
let mut nums = vec![1, 1, 5];
58+
Solution::next_permutation(&mut nums);
59+
assert_eq!(nums, vec![1, 5, 1]);
60+
}
61+
62+
#[test]
63+
fn test_next_permutation4() {
64+
let mut nums = vec![1];
65+
Solution::next_permutation(&mut nums);
66+
assert_eq!(nums, vec![1]);
67+
}
68+
69+
#[test]
70+
fn test_next_permutation5() {
71+
let mut nums = vec![1, 3, 2];
72+
Solution::next_permutation(&mut nums);
73+
assert_eq!(nums, vec![2, 1, 3]);
74+
}
75+
76+
#[test]
77+
fn test_next_permutation6() {
78+
let mut nums = vec![4, 2, 0, 2, 3, 2, 0];
79+
Solution::next_permutation(&mut nums);
80+
assert_eq!(nums, vec![4, 2, 0, 3, 0, 2, 2]);
81+
}
82+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// @leetup=custom
2+
// @leetup=info id=1524 lang=rust slug=number-of-sub-arrays-with-odd-sum
3+
4+
/*
5+
* Given an array of integers `arr`, return *the number of subarrays with an
6+
* odd sum*.
7+
*
8+
* Since the answer can be very large, return it modulo `109 + 7`.
9+
*
10+
*
11+
* Example 1:
12+
*
13+
* Input: arr = [1,3,5]
14+
* Output: 4
15+
* Explanation: All subarrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]]
16+
* All sub-arrays sum are [1,4,9,3,8,5].
17+
* Odd sums are [1,9,3,5] so the answer is 4.
18+
*
19+
* Example 2:
20+
*
21+
* Input: arr = [2,4,6]
22+
* Output: 0
23+
* Explanation: All subarrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]]
24+
* All sub-arrays sum are [2,6,12,4,10,6].
25+
* All sub-arrays have even sum and the answer is 0.
26+
*
27+
* Example 3:
28+
*
29+
* Input: arr = [1,2,3,4,5,6,7]
30+
* Output: 16
31+
*
32+
*
33+
* Constraints:
34+
*
35+
* * `1 <= arr.length <= 105`
36+
* * `1 <= arr[i] <= 100`
37+
*
38+
*/
39+
// @leetup=custom
40+
// @leetup=code
41+
42+
impl Solution {
43+
pub fn num_of_subarrays(arr: Vec<i32>) -> i32 {
44+
const MOD: i32 = 1_000_000_007;
45+
46+
let [mut even, mut odd] = [1, 0];
47+
let mut sum = 0;
48+
let mut ans = 0;
49+
for num in arr.iter() {
50+
sum += num;
51+
52+
if sum % 2 == 0 {
53+
even += 1;
54+
ans += odd % MOD
55+
} else {
56+
odd += 1;
57+
ans += even % MOD
58+
}
59+
}
60+
61+
ans
62+
}
63+
}
64+
65+
struct Solution {}
66+
67+
#[cfg(test)]
68+
mod tests {
69+
use super::*;
70+
use crate::{vec_string, vec_vec_char};
71+
72+
#[test]
73+
fn test_num_of_subarrays() {
74+
assert_eq!(Solution::num_of_subarrays(vec![1, 3, 5]), 4);
75+
}
76+
77+
#[test]
78+
fn test_num_of_subarrays2() {
79+
assert_eq!(Solution::num_of_subarrays(vec![2, 4, 6]), 0);
80+
}
81+
82+
#[test]
83+
fn test_num_of_subarrays3() {
84+
assert_eq!(Solution::num_of_subarrays(vec![1, 2, 3, 4, 5, 6, 7]), 16);
85+
}
86+
}

0 commit comments

Comments
 (0)