Skip to content

Commit 25c63d9

Browse files
committed
feat: add rust solutions to lc problems: No.3216~3219
1 parent 9ba8d36 commit 25c63d9

File tree

12 files changed

+328
-0
lines changed

12 files changed

+328
-0
lines changed

solution/3200-3299/3216.Lexicographically Smallest String After a Swap/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,25 @@ function getSmallestString(s: string): string {
161161
}
162162
```
163163

164+
#### Rust
165+
166+
```rust
167+
impl Solution {
168+
pub fn get_smallest_string(s: String) -> String {
169+
let mut cs: Vec<u8> = s.into_bytes();
170+
let n = cs.len();
171+
for i in 1..n {
172+
let (a, b) = (cs[i - 1], cs[i]);
173+
if a > b && a % 2 == b % 2 {
174+
cs.swap(i - 1, i);
175+
return String::from_utf8(cs).unwrap();
176+
}
177+
}
178+
String::from_utf8(cs).unwrap()
179+
}
180+
}
181+
```
182+
164183
<!-- tabs:end -->
165184

166185
<!-- solution:end -->

solution/3200-3299/3216.Lexicographically Smallest String After a Swap/README_EN.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,25 @@ function getSmallestString(s: string): string {
159159
}
160160
```
161161

162+
#### Rust
163+
164+
```rust
165+
impl Solution {
166+
pub fn get_smallest_string(s: String) -> String {
167+
let mut cs: Vec<u8> = s.into_bytes();
168+
let n = cs.len();
169+
for i in 1..n {
170+
let (a, b) = (cs[i - 1], cs[i]);
171+
if a > b && a % 2 == b % 2 {
172+
cs.swap(i - 1, i);
173+
return String::from_utf8(cs).unwrap();
174+
}
175+
}
176+
String::from_utf8(cs).unwrap()
177+
}
178+
}
179+
```
180+
162181
<!-- tabs:end -->
163182

164183
<!-- solution:end -->
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
impl Solution {
2+
pub fn get_smallest_string(s: String) -> String {
3+
let mut cs: Vec<u8> = s.into_bytes();
4+
let n = cs.len();
5+
for i in 1..n {
6+
let (a, b) = (cs[i - 1], cs[i]);
7+
if a > b && a % 2 == b % 2 {
8+
cs.swap(i - 1, i);
9+
return String::from_utf8(cs).unwrap();
10+
}
11+
}
12+
String::from_utf8(cs).unwrap()
13+
}
14+
}

solution/3200-3299/3217.Delete Nodes From Linked List Present in Array/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,47 @@ function modifiedList(nums: number[], head: ListNode | null): ListNode | null {
237237
}
238238
```
239239

240+
#### Rust
241+
242+
```rust
243+
// Definition for singly-linked list.
244+
// #[derive(PartialEq, Eq, Clone, Debug)]
245+
// pub struct ListNode {
246+
// pub val: i32,
247+
// pub next: Option<Box<ListNode>>
248+
// }
249+
//
250+
// impl ListNode {
251+
// #[inline]
252+
// fn new(val: i32) -> Self {
253+
// ListNode {
254+
// next: None,
255+
// val
256+
// }
257+
// }
258+
// }
259+
260+
use std::collections::HashSet;
261+
262+
impl Solution {
263+
pub fn modified_list(nums: Vec<i32>, head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
264+
let s: HashSet<i32> = nums.into_iter().collect();
265+
let mut dummy = Box::new(ListNode { val: 0, next: head });
266+
let mut pre = &mut dummy;
267+
268+
while let Some(ref mut node) = pre.next {
269+
if s.contains(&node.val) {
270+
pre.next = node.next.take();
271+
} else {
272+
pre = pre.next.as_mut().unwrap();
273+
}
274+
}
275+
276+
dummy.next
277+
}
278+
}
279+
```
280+
240281
<!-- tabs:end -->
241282

242283
<!-- solution:end -->

solution/3200-3299/3217.Delete Nodes From Linked List Present in Array/README_EN.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,47 @@ function modifiedList(nums: number[], head: ListNode | null): ListNode | null {
235235
}
236236
```
237237

238+
#### Rust
239+
240+
```rust
241+
// Definition for singly-linked list.
242+
// #[derive(PartialEq, Eq, Clone, Debug)]
243+
// pub struct ListNode {
244+
// pub val: i32,
245+
// pub next: Option<Box<ListNode>>
246+
// }
247+
//
248+
// impl ListNode {
249+
// #[inline]
250+
// fn new(val: i32) -> Self {
251+
// ListNode {
252+
// next: None,
253+
// val
254+
// }
255+
// }
256+
// }
257+
258+
use std::collections::HashSet;
259+
260+
impl Solution {
261+
pub fn modified_list(nums: Vec<i32>, head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
262+
let s: HashSet<i32> = nums.into_iter().collect();
263+
let mut dummy = Box::new(ListNode { val: 0, next: head });
264+
let mut pre = &mut dummy;
265+
266+
while let Some(ref mut node) = pre.next {
267+
if s.contains(&node.val) {
268+
pre.next = node.next.take();
269+
} else {
270+
pre = pre.next.as_mut().unwrap();
271+
}
272+
}
273+
274+
dummy.next
275+
}
276+
}
277+
```
278+
238279
<!-- tabs:end -->
239280

240281
<!-- solution:end -->
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 {
12+
// next: None,
13+
// val
14+
// }
15+
// }
16+
// }
17+
18+
use std::collections::HashSet;
19+
20+
impl Solution {
21+
pub fn modified_list(nums: Vec<i32>, head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
22+
let s: HashSet<i32> = nums.into_iter().collect();
23+
let mut dummy = Box::new(ListNode { val: 0, next: head });
24+
let mut pre = &mut dummy;
25+
26+
while let Some(ref mut node) = pre.next {
27+
if s.contains(&node.val) {
28+
pre.next = node.next.take();
29+
} else {
30+
pre = pre.next.as_mut().unwrap();
31+
}
32+
}
33+
34+
dummy.next
35+
}
36+
}

solution/3200-3299/3218.Minimum Cost for Cutting Cake I/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,34 @@ function minimumCost(m: number, n: number, horizontalCut: number[], verticalCut:
233233
}
234234
```
235235

236+
#### Rust
237+
238+
```rust
239+
impl Solution {
240+
pub fn minimum_cost(m: i32, n: i32, mut horizontal_cut: Vec<i32>, mut vertical_cut: Vec<i32>) -> i32 {
241+
horizontal_cut.sort();
242+
vertical_cut.sort();
243+
let (mut i, mut j) = ((m - 2) as isize, (n - 2) as isize);
244+
let (mut h, mut v) = (1, 1);
245+
let mut ans = 0;
246+
247+
while i >= 0 || j >= 0 {
248+
if j < 0 || (i >= 0 && horizontal_cut[i as usize] > vertical_cut[j as usize]) {
249+
ans += horizontal_cut[i as usize] * v;
250+
i -= 1;
251+
h += 1;
252+
} else {
253+
ans += vertical_cut[j as usize] * h;
254+
j -= 1;
255+
v += 1;
256+
}
257+
}
258+
259+
ans
260+
}
261+
}
262+
```
263+
236264
<!-- tabs:end -->
237265

238266
<!-- solution:end -->

solution/3200-3299/3218.Minimum Cost for Cutting Cake I/README_EN.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,34 @@ function minimumCost(m: number, n: number, horizontalCut: number[], verticalCut:
231231
}
232232
```
233233

234+
#### Rust
235+
236+
```rust
237+
impl Solution {
238+
pub fn minimum_cost(m: i32, n: i32, mut horizontal_cut: Vec<i32>, mut vertical_cut: Vec<i32>) -> i32 {
239+
horizontal_cut.sort();
240+
vertical_cut.sort();
241+
let (mut i, mut j) = ((m - 2) as isize, (n - 2) as isize);
242+
let (mut h, mut v) = (1, 1);
243+
let mut ans = 0;
244+
245+
while i >= 0 || j >= 0 {
246+
if j < 0 || (i >= 0 && horizontal_cut[i as usize] > vertical_cut[j as usize]) {
247+
ans += horizontal_cut[i as usize] * v;
248+
i -= 1;
249+
h += 1;
250+
} else {
251+
ans += vertical_cut[j as usize] * h;
252+
j -= 1;
253+
v += 1;
254+
}
255+
}
256+
257+
ans
258+
}
259+
}
260+
```
261+
234262
<!-- tabs:end -->
235263

236264
<!-- solution:end -->
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
impl Solution {
2+
pub fn minimum_cost(m: i32, n: i32, mut horizontal_cut: Vec<i32>, mut vertical_cut: Vec<i32>) -> i32 {
3+
horizontal_cut.sort();
4+
vertical_cut.sort();
5+
let (mut i, mut j) = ((m - 2) as isize, (n - 2) as isize);
6+
let (mut h, mut v) = (1, 1);
7+
let mut ans = 0;
8+
9+
while i >= 0 || j >= 0 {
10+
if j < 0 || (i >= 0 && horizontal_cut[i as usize] > vertical_cut[j as usize]) {
11+
ans += horizontal_cut[i as usize] * v;
12+
i -= 1;
13+
h += 1;
14+
} else {
15+
ans += vertical_cut[j as usize] * h;
16+
j -= 1;
17+
v += 1;
18+
}
19+
}
20+
21+
ans
22+
}
23+
}

solution/3200-3299/3219.Minimum Cost for Cutting Cake II/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,34 @@ function minimumCost(m: number, n: number, horizontalCut: number[], verticalCut:
232232
}
233233
```
234234

235+
#### Rust
236+
237+
```rust
238+
impl Solution {
239+
pub fn minimum_cost(m: i32, n: i32, mut horizontal_cut: Vec<i32>, mut vertical_cut: Vec<i32>) -> i64 {
240+
horizontal_cut.sort();
241+
vertical_cut.sort();
242+
let (mut i, mut j) = ((m - 2) as isize, (n - 2) as isize);
243+
let (mut h, mut v) = (1_i64, 1_i64);
244+
let mut ans: i64 = 0;
245+
246+
while i >= 0 || j >= 0 {
247+
if j < 0 || (i >= 0 && horizontal_cut[i as usize] > vertical_cut[j as usize]) {
248+
ans += horizontal_cut[i as usize] as i64 * v;
249+
i -= 1;
250+
h += 1;
251+
} else {
252+
ans += vertical_cut[j as usize] as i64 * h;
253+
j -= 1;
254+
v += 1;
255+
}
256+
}
257+
258+
ans
259+
}
260+
}
261+
```
262+
235263
<!-- tabs:end -->
236264

237265
<!-- solution:end -->

0 commit comments

Comments
 (0)