Skip to content

Commit 6474adf

Browse files
committed
Added tasks 21-78
1 parent 31df4cc commit 6474adf

File tree

31 files changed

+2291
-0
lines changed

31 files changed

+2291
-0
lines changed

README.md

Lines changed: 97 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Rust/LeetCode-in-Rust?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Rust/LeetCode-in-Rust)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Rust/LeetCode-in-Rust?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Rust/LeetCode-in-Rust/fork)
3+
4+
## 21\. Merge Two Sorted Lists
5+
6+
Easy
7+
8+
Merge two sorted linked lists and return it as a **sorted** list. The list should be made by splicing together the nodes of the first two lists.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2020/10/03/merge_ex1.jpg)
13+
14+
**Input:** l1 = [1,2,4], l2 = [1,3,4]
15+
16+
**Output:** [1,1,2,3,4,4]
17+
18+
**Example 2:**
19+
20+
**Input:** l1 = [], l2 = []
21+
22+
**Output:** []
23+
24+
**Example 3:**
25+
26+
**Input:** l1 = [], l2 = [0]
27+
28+
**Output:** [0]
29+
30+
**Constraints:**
31+
32+
* The number of nodes in both lists is in the range `[0, 50]`.
33+
* `-100 <= Node.val <= 100`
34+
* Both `l1` and `l2` are sorted in **non-decreasing** order.
35+
36+
## Solution
37+
38+
```rust
39+
// Definition for singly-linked list.
40+
// pub struct ListNode {
41+
// pub val: i32,
42+
// pub next: Option<Box<ListNode>>
43+
// }
44+
//
45+
// impl ListNode {
46+
// #[inline]
47+
// fn new(val: i32) -> Self {
48+
// ListNode {
49+
// next: None,
50+
// val
51+
// }
52+
// }
53+
// }
54+
impl Solution {
55+
pub fn merge_two_lists(
56+
mut l1: Option<Box<ListNode>>,
57+
mut l2: Option<Box<ListNode>>,
58+
) -> Option<Box<ListNode>> {
59+
let mut dummy = Some(Box::new(ListNode::new(-1)));
60+
let mut current = &mut dummy;
61+
62+
while l1.is_some() || l2.is_some() {
63+
if let (Some(ref l1_node), Some(ref l2_node)) = (&l1, &l2) {
64+
if l1_node.val <= l2_node.val {
65+
current.as_mut().unwrap().next = l1.take();
66+
l1 = current.as_mut().unwrap().next.as_mut().unwrap().next.take();
67+
} else {
68+
current.as_mut().unwrap().next = l2.take();
69+
l2 = current.as_mut().unwrap().next.as_mut().unwrap().next.take();
70+
}
71+
} else if l1.is_some() {
72+
current.as_mut().unwrap().next = l1.take();
73+
} else {
74+
current.as_mut().unwrap().next = l2.take();
75+
}
76+
current = &mut current.as_mut().unwrap().next;
77+
}
78+
79+
dummy.unwrap().next
80+
}
81+
}
82+
```
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Rust/LeetCode-in-Rust?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Rust/LeetCode-in-Rust)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Rust/LeetCode-in-Rust?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Rust/LeetCode-in-Rust/fork)
3+
4+
## 22\. Generate Parentheses
5+
6+
Medium
7+
8+
Given `n` pairs of parentheses, write a function to _generate all combinations of well-formed parentheses_.
9+
10+
**Example 1:**
11+
12+
**Input:** n = 3
13+
14+
**Output:** ["((()))","(()())","(())()","()(())","()()()"]
15+
16+
**Example 2:**
17+
18+
**Input:** n = 1
19+
20+
**Output:** ["()"]
21+
22+
**Constraints:**
23+
24+
* `1 <= n <= 8`
25+
26+
## Solution
27+
28+
```rust
29+
impl Solution {
30+
pub fn generate_parenthesis(n: i32) -> Vec<String> {
31+
let mut result = Vec::new();
32+
let mut current = String::new();
33+
Solution::generate(&mut current, &mut result, n, n);
34+
result
35+
}
36+
37+
fn generate(current: &mut String, result: &mut Vec<String>, open: i32, close: i32) {
38+
if open == 0 && close == 0 {
39+
result.push(current.clone());
40+
return;
41+
}
42+
43+
if open > 0 {
44+
current.push('(');
45+
Solution::generate(current, result, open - 1, close);
46+
current.pop();
47+
}
48+
49+
if close > 0 && open < close {
50+
current.push(')');
51+
Solution::generate(current, result, open, close - 1);
52+
current.pop();
53+
}
54+
}
55+
}
56+
```
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Rust/LeetCode-in-Rust?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Rust/LeetCode-in-Rust)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Rust/LeetCode-in-Rust?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Rust/LeetCode-in-Rust/fork)
3+
4+
## 23\. Merge k Sorted Lists
5+
6+
Hard
7+
8+
You are given an array of `k` linked-lists `lists`, each linked-list is sorted in ascending order.
9+
10+
_Merge all the linked-lists into one sorted linked-list and return it._
11+
12+
**Example 1:**
13+
14+
**Input:** lists = \[\[1,4,5],[1,3,4],[2,6]]
15+
16+
**Output:** [1,1,2,3,4,4,5,6]
17+
18+
**Explanation:** The linked-lists are: [ 1->4->5, 1->3->4, 2->6 ] merging them into one sorted list: 1->1->2->3->4->4->5->6
19+
20+
**Example 2:**
21+
22+
**Input:** lists = []
23+
24+
**Output:** []
25+
26+
**Example 3:**
27+
28+
**Input:** lists = \[\[]]
29+
30+
**Output:** []
31+
32+
**Constraints:**
33+
34+
* `k == lists.length`
35+
* `0 <= k <= 10^4`
36+
* `0 <= lists[i].length <= 500`
37+
* `-10^4 <= lists[i][j] <= 10^4`
38+
* `lists[i]` is sorted in **ascending order**.
39+
* The sum of `lists[i].length` won't exceed `10^4`.
40+
41+
## Solution
42+
43+
```rust
44+
// Definition for singly-linked list.
45+
// pub struct ListNode {
46+
// pub val: i32,
47+
// pub next: Option<Box<ListNode>>
48+
// }
49+
//
50+
// impl ListNode {
51+
// #[inline]
52+
// fn new(val: i32) -> Self {
53+
// ListNode {
54+
// next: None,
55+
// val
56+
// }
57+
// }
58+
// }
59+
use std::cmp::Ordering;
60+
use std::collections::BinaryHeap;
61+
use std::cmp::Reverse;
62+
63+
struct HeapNode(Box<ListNode>);
64+
65+
impl PartialEq for HeapNode {
66+
fn eq(&self, other: &Self) -> bool {
67+
self.0.val == other.0.val
68+
}
69+
}
70+
71+
impl Eq for HeapNode {}
72+
73+
impl PartialOrd for HeapNode {
74+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
75+
Some(self.cmp(other))
76+
}
77+
}
78+
79+
impl Ord for HeapNode {
80+
fn cmp(&self, other: &Self) -> Ordering {
81+
other.0.val.cmp(&self.0.val)
82+
}
83+
}
84+
85+
impl Solution {
86+
pub fn merge_k_lists(lists: Vec<Option<Box<ListNode>>>) -> Option<Box<ListNode>> {
87+
let mut heap = BinaryHeap::new();
88+
89+
for list in lists {
90+
if let Some(node) = list {
91+
heap.push(HeapNode(node));
92+
}
93+
}
94+
95+
let mut dummy = Box::new(ListNode::new(0));
96+
let mut tail = &mut dummy;
97+
98+
while let Some(HeapNode(mut node)) = heap.pop() {
99+
if let Some(next) = node.next.take() {
100+
heap.push(HeapNode(next));
101+
}
102+
tail.next = Some(node);
103+
tail = tail.next.as_mut().unwrap();
104+
}
105+
106+
dummy.next
107+
}
108+
}
109+
```
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Rust/LeetCode-in-Rust?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Rust/LeetCode-in-Rust)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Rust/LeetCode-in-Rust?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Rust/LeetCode-in-Rust/fork)
3+
4+
## 24\. Swap Nodes in Pairs
5+
6+
Medium
7+
8+
Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.)
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2020/10/03/swap_ex1.jpg)
13+
14+
**Input:** head = [1,2,3,4]
15+
16+
**Output:** [2,1,4,3]
17+
18+
**Example 2:**
19+
20+
**Input:** head = []
21+
22+
**Output:** []
23+
24+
**Example 3:**
25+
26+
**Input:** head = [1]
27+
28+
**Output:** [1]
29+
30+
**Constraints:**
31+
32+
* The number of nodes in the list is in the range `[0, 100]`.
33+
* `0 <= Node.val <= 100`
34+
35+
## Solution
36+
37+
```rust
38+
// Definition for singly-linked list.
39+
// pub struct ListNode {
40+
// pub val: i32,
41+
// pub next: Option<Box<ListNode>>
42+
// }
43+
//
44+
// impl ListNode {
45+
// #[inline]
46+
// fn new(val: i32) -> Self {
47+
// ListNode {
48+
// next: None,
49+
// val
50+
// }
51+
// }
52+
// }
53+
impl Solution {
54+
pub fn swap_pairs(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
55+
let mut dummy = ListNode {
56+
val: 0,
57+
next: head,
58+
};
59+
let mut current = &mut dummy;
60+
61+
while let Some(mut first) = current.next.take() {
62+
if let Some(mut second) = first.next.take() {
63+
first.next = second.next.take();
64+
second.next = Some(first);
65+
current.next = Some(second);
66+
current = current.next.as_mut().unwrap().next.as_mut().unwrap();
67+
} else {
68+
current.next = Some(first);
69+
break;
70+
}
71+
}
72+
73+
dummy.next
74+
}
75+
}
76+
```

0 commit comments

Comments
 (0)