Skip to content

Commit 4f159b6

Browse files
committed
Add 2 solutions
1 parent 52f6ba4 commit 4f159b6

File tree

6 files changed

+109
-0
lines changed

6 files changed

+109
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ LeetCode is a website that has programming-related questions that are designed t
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 |
13+
| 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 |
1314
| 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 |
15+
| 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 |
1416
| 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 |
1517
| 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 |
1618
| 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 |
@@ -32,6 +34,7 @@ LeetCode is a website that has programming-related questions that are designed t
3234
| 713 | Subarray Product Less Than K | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/num_subarray_product_less_than_k.rs) | [Leetcode](https://leetcode.com/problems/subarray-product-less-than-k/) | Medium |
3335
| 774 | Minimize Max Distance to Gas Station | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/minmax_gas_dist.rs) | [Leetcode](https://leetcode.com/problems/minimize-max-distance-to-gas-station/) | Medium |
3436
| 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 |
37+
| 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 |
3538
| 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 |
3639
| 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 |
3740
| 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 |

src/leetcode/challenge/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ mod num_distinct;
88
mod num_unique_emails;
99
mod shortest_distance;
1010
mod shortest_path;
11+
mod sort_array_by_parity_ii;
1112
mod spiral_order;
1213
mod tictactoe;
1314
mod tribonacci;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use std::collections::VecDeque;
2+
3+
// 922. Sort Array By Parity II, Easy
4+
// https://leetcode.com/problems/sort-array-by-parity-ii/
5+
impl Solution {
6+
pub fn sort_array_by_parity_ii(nums: Vec<i32>) -> Vec<i32> {
7+
let n = nums.len();
8+
let mut ans: Vec<i32> = Vec::new();
9+
10+
let mut p1: VecDeque<i32> = VecDeque::new();
11+
let mut p2: VecDeque<i32> = VecDeque::new();
12+
13+
for num in nums {
14+
if num % 2 == 0 {
15+
p1.push_back(num);
16+
} else {
17+
p2.push_back(num);
18+
}
19+
}
20+
21+
for i in 0..n {
22+
if i % 2 == 0 {
23+
ans.push(p1.pop_front().unwrap());
24+
} else {
25+
ans.push(p2.pop_front().unwrap());
26+
}
27+
}
28+
29+
return ans;
30+
}
31+
}
32+
33+
struct Solution {}
34+
35+
#[cfg(test)]
36+
mod tests {
37+
use super::*;
38+
39+
#[test]
40+
fn test_sort_array_by_parity_ii() {
41+
assert_eq!(Solution::sort_array_by_parity_ii(vec![4, 2, 5, 7]).len(), 4);
42+
}
43+
44+
#[test]
45+
fn test_sort_array_by_parity_ii2() {
46+
assert_eq!(Solution::sort_array_by_parity_ii(vec![2, 3]), vec![2, 3]);
47+
}
48+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Definition for singly-linked list.
2+
#[derive(PartialEq, Eq, Clone, Debug)]
3+
pub struct ListNode {
4+
pub val: i32,
5+
pub next: Option<Box<ListNode>>,
6+
}
7+
8+
impl ListNode {
9+
#[inline]
10+
fn new(val: i32) -> Self {
11+
ListNode { next: None, val }
12+
}
13+
}
14+
15+
pub trait ListMaker {
16+
fn link(val: i32, next: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
17+
Some(Box::new(ListNode { val, next }))
18+
}
19+
}
20+
21+
impl ListMaker for Option<Box<ListNode>> {}
22+
23+
// 2. Add Two Numbers, Medium
24+
// https://leetcode.com/problems/add-two-numbers/
25+
impl Solution {
26+
pub fn add_two_numbers(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
27+
let mut sum: Option<Box<ListNode>> = None;
28+
29+
let mut p1 = &l1;
30+
let mut p2 = &l2;
31+
let mut p3 = &mut sum;
32+
let mut carry = 0;
33+
34+
while p1.is_some() || p2.is_some() || carry != 0 {
35+
let mut val = carry;
36+
37+
if let Some(n1) = p1.as_ref() {
38+
val += n1.val;
39+
p1 = &n1.next;
40+
}
41+
42+
if let Some(n2) = p2.as_ref() {
43+
val += n2.val;
44+
p2 = &n2.next;
45+
}
46+
47+
carry = val / 10;
48+
*p3 = Option::<Box<ListNode>>::link(val % 10, None);
49+
p3 = &mut p3.as_mut().unwrap().next;
50+
}
51+
sum
52+
}
53+
}
54+
55+
struct Solution {}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mod add_two_numbers;

src/leetcode/top_interview/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod array_and_string;
22
mod dynamic_programming;
3+
mod linked_list;
34
mod others;
45
mod sorting;

0 commit comments

Comments
 (0)