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 9d40423

Browse files
committed
Add 4 solutions
1 parent 35f10b4 commit 9d40423

File tree

6 files changed

+316
-1
lines changed

6 files changed

+316
-1
lines changed

β€ŽREADME.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
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 (67)
9+
## Solutions (71)
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 |
@@ -16,6 +16,8 @@ LeetCode is a website that has programming-related questions that are designed t
1616
| 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 |
1717
| 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 |
1818
| 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 |
19+
| 51 | N-Queens | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/solve_n_queens.rs) | [Leetcode](https://leetcode.com/problems/n-queens/) | Hard |
20+
| 52 | N-Queens II | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/total_n_queens.rs) | [Leetcode](https://leetcode.com/problems/n-queens-ii/) | Hard |
1921
| 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 |
2022
| 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 |
2123
| 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 |
@@ -55,6 +57,7 @@ LeetCode is a website that has programming-related questions that are designed t
5557
| 875 | Koko Eating Bananas | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/min_eating_speed.rs) | [Leetcode](https://leetcode.com/problems/koko-eating-bananas/) | Medium |
5658
| 922 | Sort Array By Parity II | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/sort_array_by_parity_ii.rs) | [Leetcode](https://leetcode.com/problems/sort-array-by-parity-ii/) | Easy |
5759
| 929 | Unique Email Addresses | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/num_unique_emails.rs) | [Leetcode](https://leetcode.com/problems/unique-email-addresses/) | Easy |
60+
| 930 | Binary Subarrays With Sum | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/num_subarrays_with_sum.rs) | [Leetcode](https://leetcode.com/problems/binary-subarrays-with-sum/) | Medium |
5861
| 991 | Broken Calculator | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/broken_calc.rs) | [Leetcode](https://leetcode.com/problems/broken-calculator/) | Medium |
5962
| 1035 | Uncrossed Lines | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/max_uncrossed_lines.rs) | [Leetcode](https://leetcode.com/problems/uncrossed-lines/) | Medium |
6063
| 1134 | Armstrong Number | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/is_good_array.rs) | [Leetcode](https://leetcode.com/problems/armstrong-number/) | Easy |
@@ -68,6 +71,7 @@ LeetCode is a website that has programming-related questions that are designed t
6871
| 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 |
6972
| 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 |
7073
| 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 |
74+
| 1570 | Dot Product of Two Sparse Vectors | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/dot_product.rs) | [Leetcode](https://leetcode.com/problems/dot-product-of-two-sparse-vectors/) | Medium |
7175
| 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 |
7276
| 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 |
7377
| 2022 | Convert 1D Array Into 2D Array | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/contest/biweekly_62.rs) | [Leetcode](https://leetcode.com/problems/convert-1d-array-into-2d-array/) | Easy |

β€Žsrc/leetcode/problem/dot_product.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// 1570. Dot Product of Two Sparse Vectors, Medium
2+
// https://leetcode.com/problems/dot-product-of-two-sparse-vectors/
3+
impl SparseVector {
4+
fn new(nums: Vec<i32>) -> Self {
5+
SparseVector { nums }
6+
}
7+
8+
fn dot_product(&self, vec: SparseVector) -> i32 {
9+
let mut result = 0;
10+
11+
for i in 0..self.nums.len() {
12+
result += self.nums[i] * vec.nums[i];
13+
}
14+
15+
result
16+
}
17+
}
18+
19+
struct SparseVector {
20+
nums: Vec<i32>,
21+
}

β€Žsrc/leetcode/problem/exist.rs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
use std::collections::{HashSet, VecDeque};
2+
3+
impl Solution {
4+
pub fn exist(board: Vec<Vec<char>>, word: String) -> bool {
5+
if word.len() == 0 {
6+
return true;
7+
}
8+
9+
let [n, m] = [board.len(), board[0].len()];
10+
11+
fn dfs(board: &Vec<Vec<char>>, word: &String, idx: i32, pos: (i32, i32), seen: &mut Vec<Vec<i32>>, ans: &mut bool) {
12+
let [n, m] = [board.len(), board[0].len()];
13+
let (i, j) = pos;
14+
15+
if idx >= word.len() as i32 {
16+
*ans = true;
17+
return;
18+
}
19+
20+
if 0 > i || i >= n as i32 || 0 > j || j >= m as i32 {
21+
return;
22+
}
23+
if seen[i as usize][j as usize] == 1 {
24+
return;
25+
}
26+
if board[i as usize][j as usize] != word.chars().nth(idx as usize).unwrap() {
27+
return;
28+
}
29+
30+
seen[i as usize][j as usize] = 1;
31+
let dirs = vec![(0, 1), (0, -1), (1, 0), (-1, 0)];
32+
for dir in dirs.iter() {
33+
let pos = (i + dir.0, j + dir.1);
34+
dfs(board, word, idx + 1, (i + dir.0, j + dir.1), seen, ans);
35+
}
36+
seen[i as usize][j as usize] = 0;
37+
}
38+
39+
let mut ans = false;
40+
for i in 0..n {
41+
for j in 0..m {
42+
let [i, j] = [i as i32, j as i32];
43+
let mut seen = vec![vec![0; m]; n];
44+
dfs(&board, &word, 0, (i, j), &mut seen, &mut ans);
45+
}
46+
}
47+
48+
ans
49+
}
50+
}
51+
52+
struct Solution {}
53+
54+
#[cfg(test)]
55+
mod tests {
56+
use super::*;
57+
use crate::{vec_vec_char, vec_vec_i32};
58+
59+
#[test]
60+
fn test_exist() {
61+
assert_eq!(
62+
Solution::exist(vec_vec_char![['A', 'B', 'C', 'E'], ['S', 'F', 'C', 'S'], ['A', 'D', 'E', 'E']], "ABCCED".to_string()),
63+
true
64+
);
65+
}
66+
67+
#[test]
68+
fn test_exist2() {
69+
assert_eq!(
70+
Solution::exist(vec_vec_char![['A', 'B', 'C', 'E'], ['S', 'F', 'C', 'S'], ['A', 'D', 'E', 'E']], "SEE".to_string()),
71+
true
72+
);
73+
}
74+
75+
#[test]
76+
fn test_exist3() {
77+
assert_eq!(
78+
Solution::exist(vec_vec_char![['A', 'B', 'C', 'E'], ['S', 'F', 'C', 'S'], ['A', 'D', 'E', 'E']], "ABCB".to_string()),
79+
false
80+
);
81+
}
82+
83+
#[test]
84+
fn test_exist4() {
85+
assert_eq!(Solution::exist(vec_vec_char![['a']], "a".to_string()), true);
86+
}
87+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use std::collections::HashMap;
2+
3+
// 930. Binary Subarrays With Sum, Medium
4+
// https://leetcode.com/problems/binary-subarrays-with-sum/
5+
impl Solution {
6+
pub fn num_subarrays_with_sum(nums: Vec<i32>, goal: i32) -> i32 {
7+
let mut ans = 0;
8+
let mut prefix_sum = 0;
9+
let mut prefix_sum_map = HashMap::new();
10+
for num in nums {
11+
prefix_sum += num;
12+
if prefix_sum == goal {
13+
ans += 1;
14+
}
15+
if let Some(count) = prefix_sum_map.get(&(prefix_sum - goal)) {
16+
ans += *count;
17+
}
18+
19+
prefix_sum_map.entry(prefix_sum).and_modify(|e| *e += 1).or_insert(1);
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::num_subarrays_with_sum(vec![1, 0, 1, 0, 1], 2), 4);
35+
}
36+
37+
#[test]
38+
fn test_find_duplicates2() {
39+
assert_eq!(Solution::num_subarrays_with_sum(vec![0, 0, 0, 0, 0], 0), 15);
40+
}
41+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
use std::collections::{HashSet, VecDeque};
2+
3+
// 51. N-Queens, Hard
4+
// https://leetcode.com/problems/n-queens/
5+
impl Solution {
6+
pub fn solve_n_queens(n: i32) -> Vec<Vec<String>> {
7+
let mut board: Vec<Vec<i32>> = vec![vec![0; n as usize]; n as usize];
8+
9+
// Check if queen can be placed
10+
fn is_valid_placement(board: &Vec<Vec<i32>>, n: i32, i: i32, j: i32) -> bool {
11+
let mut queue = VecDeque::new();
12+
13+
queue.push_back((i, j, (1, 0)));
14+
queue.push_back((i, j, (1, 1)));
15+
queue.push_back((i, j, (0, 1)));
16+
queue.push_back((i, j, (-1, 1)));
17+
queue.push_back((i, j, (-1, 0)));
18+
queue.push_back((i, j, (-1, -1)));
19+
queue.push_back((i, j, (0, -1)));
20+
queue.push_back((i, j, (1, -1)));
21+
22+
while !queue.is_empty() {
23+
let (x, y, direction) = queue.pop_front().unwrap();
24+
25+
if 0 <= x && x < n && 0 <= y && y < n {
26+
if board[x as usize][y as usize] == 1 {
27+
return false;
28+
}
29+
30+
queue.push_back((x + direction.0, y + direction.1, direction));
31+
}
32+
}
33+
34+
return true;
35+
}
36+
37+
let mut ans: Vec<Vec<Vec<i32>>> = vec![];
38+
fn backtracking(ans: &mut Vec<Vec<Vec<i32>>>, board: &mut Vec<Vec<i32>>, n: usize, placed: usize, i: usize, j: usize) {
39+
if placed == n {
40+
ans.push(board.clone());
41+
return;
42+
} else if i == n {
43+
return;
44+
} else if j == n {
45+
backtracking(ans, board, n, placed, i + 1, 0);
46+
} else {
47+
if is_valid_placement(board, n as i32, i as i32, j as i32) {
48+
board[i][j] = 1;
49+
backtracking(ans, board, n, placed + 1, i, j + 1);
50+
board[i][j] = 0;
51+
}
52+
53+
backtracking(ans, board, n, placed, i, j + 1);
54+
}
55+
}
56+
57+
backtracking(&mut ans, &mut board, n as usize, 0, 0, 0);
58+
59+
ans.iter()
60+
.map(|ans| {
61+
ans.iter()
62+
.map(|row| row.iter().map(|ans| if *ans == 1 { "Q".to_string() } else { ".".to_string() }).collect::<String>())
63+
.collect()
64+
})
65+
.collect()
66+
}
67+
}
68+
69+
struct Solution {}
70+
71+
#[cfg(test)]
72+
mod tests {
73+
use super::*;
74+
75+
#[test]
76+
fn test_find_duplicates() {
77+
assert_eq!(Solution::solve_n_queens(4).len(), 2);
78+
}
79+
80+
#[test]
81+
fn test_find_duplicates2() {
82+
assert_eq!(Solution::solve_n_queens(1).len(), 1);
83+
}
84+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
use std::collections::{HashSet, VecDeque};
2+
3+
// 52. N-Queens II, Hard
4+
// https://leetcode.com/problems/n-queens-ii/
5+
impl Solution {
6+
pub fn total_n_queens(n: i32) -> i32 {
7+
let mut board: Vec<Vec<i32>> = vec![vec![0; n as usize]; n as usize];
8+
9+
// Check if queen can be placed
10+
fn is_valid_placement(board: &Vec<Vec<i32>>, n: i32, i: i32, j: i32) -> bool {
11+
let mut queue = VecDeque::new();
12+
13+
queue.push_back((i, j, (1, 0)));
14+
queue.push_back((i, j, (1, 1)));
15+
queue.push_back((i, j, (0, 1)));
16+
queue.push_back((i, j, (-1, 1)));
17+
queue.push_back((i, j, (-1, 0)));
18+
queue.push_back((i, j, (-1, -1)));
19+
queue.push_back((i, j, (0, -1)));
20+
queue.push_back((i, j, (1, -1)));
21+
22+
while !queue.is_empty() {
23+
let (x, y, direction) = queue.pop_front().unwrap();
24+
25+
if 0 <= x && x < n && 0 <= y && y < n {
26+
if board[x as usize][y as usize] == 1 {
27+
return false;
28+
}
29+
30+
queue.push_back((x + direction.0, y + direction.1, direction));
31+
}
32+
}
33+
34+
return true;
35+
}
36+
37+
let mut ans: Vec<Vec<Vec<i32>>> = vec![];
38+
fn backtracking(ans: &mut Vec<Vec<Vec<i32>>>, board: &mut Vec<Vec<i32>>, n: usize, placed: usize, i: usize, j: usize) {
39+
if placed == n {
40+
ans.push(board.clone());
41+
return;
42+
} else if i == n {
43+
return;
44+
} else if j == n {
45+
backtracking(ans, board, n, placed, i + 1, 0);
46+
} else {
47+
if is_valid_placement(board, n as i32, i as i32, j as i32) {
48+
board[i][j] = 1;
49+
backtracking(ans, board, n, placed + 1, i, j + 1);
50+
board[i][j] = 0;
51+
}
52+
53+
backtracking(ans, board, n, placed, i, j + 1);
54+
}
55+
}
56+
57+
backtracking(&mut ans, &mut board, n as usize, 0, 0, 0);
58+
59+
ans.len() as i32
60+
}
61+
}
62+
63+
struct Solution {}
64+
65+
#[cfg(test)]
66+
mod tests {
67+
use super::*;
68+
69+
#[test]
70+
fn test_find_duplicates() {
71+
assert_eq!(Solution::total_n_queens(4), 2);
72+
}
73+
74+
#[test]
75+
fn test_find_duplicates2() {
76+
assert_eq!(Solution::total_n_queens(1), 1);
77+
}
78+
}

0 commit comments

Comments
(0)

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /