Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,25 @@ function getSmallestString(s: string): string {
}
```

#### Rust

```rust
impl Solution {
pub fn get_smallest_string(s: String) -> String {
let mut cs: Vec<u8> = s.into_bytes();
let n = cs.len();
for i in 1..n {
let (a, b) = (cs[i - 1], cs[i]);
if a > b && a % 2 == b % 2 {
cs.swap(i - 1, i);
return String::from_utf8(cs).unwrap();
}
}
String::from_utf8(cs).unwrap()
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,25 @@ function getSmallestString(s: string): string {
}
```

#### Rust

```rust
impl Solution {
pub fn get_smallest_string(s: String) -> String {
let mut cs: Vec<u8> = s.into_bytes();
let n = cs.len();
for i in 1..n {
let (a, b) = (cs[i - 1], cs[i]);
if a > b && a % 2 == b % 2 {
cs.swap(i - 1, i);
return String::from_utf8(cs).unwrap();
}
}
String::from_utf8(cs).unwrap()
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
impl Solution {
pub fn get_smallest_string(s: String) -> String {
let mut cs: Vec<u8> = s.into_bytes();
let n = cs.len();
for i in 1..n {
let (a, b) = (cs[i - 1], cs[i]);
if a > b && a % 2 == b % 2 {
cs.swap(i - 1, i);
return String::from_utf8(cs).unwrap();
}
}
String::from_utf8(cs).unwrap()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,47 @@ function modifiedList(nums: number[], head: ListNode | null): ListNode | null {
}
```

#### Rust

```rust
// Definition for singly-linked list.
// #[derive(PartialEq, Eq, Clone, Debug)]
// pub struct ListNode {
// pub val: i32,
// pub next: Option<Box<ListNode>>
// }
//
// impl ListNode {
// #[inline]
// fn new(val: i32) -> Self {
// ListNode {
// next: None,
// val
// }
// }
// }

use std::collections::HashSet;

impl Solution {
pub fn modified_list(nums: Vec<i32>, head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let s: HashSet<i32> = nums.into_iter().collect();
let mut dummy = Box::new(ListNode { val: 0, next: head });
let mut pre = &mut dummy;

while let Some(ref mut node) = pre.next {
if s.contains(&node.val) {
pre.next = node.next.take();
} else {
pre = pre.next.as_mut().unwrap();
}
}

dummy.next
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,47 @@ function modifiedList(nums: number[], head: ListNode | null): ListNode | null {
}
```

#### Rust

```rust
// Definition for singly-linked list.
// #[derive(PartialEq, Eq, Clone, Debug)]
// pub struct ListNode {
// pub val: i32,
// pub next: Option<Box<ListNode>>
// }
//
// impl ListNode {
// #[inline]
// fn new(val: i32) -> Self {
// ListNode {
// next: None,
// val
// }
// }
// }

use std::collections::HashSet;

impl Solution {
pub fn modified_list(nums: Vec<i32>, head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let s: HashSet<i32> = nums.into_iter().collect();
let mut dummy = Box::new(ListNode { val: 0, next: head });
let mut pre = &mut dummy;

while let Some(ref mut node) = pre.next {
if s.contains(&node.val) {
pre.next = node.next.take();
} else {
pre = pre.next.as_mut().unwrap();
}
}

dummy.next
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Definition for singly-linked list.
// #[derive(PartialEq, Eq, Clone, Debug)]
// pub struct ListNode {
// pub val: i32,
// pub next: Option<Box<ListNode>>
// }
//
// impl ListNode {
// #[inline]
// fn new(val: i32) -> Self {
// ListNode {
// next: None,
// val
// }
// }
// }

use std::collections::HashSet;

impl Solution {
pub fn modified_list(nums: Vec<i32>, head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let s: HashSet<i32> = nums.into_iter().collect();
let mut dummy = Box::new(ListNode { val: 0, next: head });
let mut pre = &mut dummy;

while let Some(ref mut node) = pre.next {
if s.contains(&node.val) {
pre.next = node.next.take();
} else {
pre = pre.next.as_mut().unwrap();
}
}

dummy.next
}
}
28 changes: 28 additions & 0 deletions solution/3200-3299/3218.Minimum Cost for Cutting Cake I/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,34 @@ function minimumCost(m: number, n: number, horizontalCut: number[], verticalCut:
}
```

#### Rust

```rust
impl Solution {
pub fn minimum_cost(m: i32, n: i32, mut horizontal_cut: Vec<i32>, mut vertical_cut: Vec<i32>) -> i32 {
horizontal_cut.sort();
vertical_cut.sort();
let (mut i, mut j) = ((m - 2) as isize, (n - 2) as isize);
let (mut h, mut v) = (1, 1);
let mut ans = 0;

while i >= 0 || j >= 0 {
if j < 0 || (i >= 0 && horizontal_cut[i as usize] > vertical_cut[j as usize]) {
ans += horizontal_cut[i as usize] * v;
i -= 1;
h += 1;
} else {
ans += vertical_cut[j as usize] * h;
j -= 1;
v += 1;
}
}

ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,34 @@ function minimumCost(m: number, n: number, horizontalCut: number[], verticalCut:
}
```

#### Rust

```rust
impl Solution {
pub fn minimum_cost(m: i32, n: i32, mut horizontal_cut: Vec<i32>, mut vertical_cut: Vec<i32>) -> i32 {
horizontal_cut.sort();
vertical_cut.sort();
let (mut i, mut j) = ((m - 2) as isize, (n - 2) as isize);
let (mut h, mut v) = (1, 1);
let mut ans = 0;

while i >= 0 || j >= 0 {
if j < 0 || (i >= 0 && horizontal_cut[i as usize] > vertical_cut[j as usize]) {
ans += horizontal_cut[i as usize] * v;
i -= 1;
h += 1;
} else {
ans += vertical_cut[j as usize] * h;
j -= 1;
v += 1;
}
}

ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
impl Solution {
pub fn minimum_cost(m: i32, n: i32, mut horizontal_cut: Vec<i32>, mut vertical_cut: Vec<i32>) -> i32 {
horizontal_cut.sort();
vertical_cut.sort();
let (mut i, mut j) = ((m - 2) as isize, (n - 2) as isize);
let (mut h, mut v) = (1, 1);
let mut ans = 0;

while i >= 0 || j >= 0 {
if j < 0 || (i >= 0 && horizontal_cut[i as usize] > vertical_cut[j as usize]) {
ans += horizontal_cut[i as usize] * v;
i -= 1;
h += 1;
} else {
ans += vertical_cut[j as usize] * h;
j -= 1;
v += 1;
}
}

ans
}
}
28 changes: 28 additions & 0 deletions solution/3200-3299/3219.Minimum Cost for Cutting Cake II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,34 @@ function minimumCost(m: number, n: number, horizontalCut: number[], verticalCut:
}
```

#### Rust

```rust
impl Solution {
pub fn minimum_cost(m: i32, n: i32, mut horizontal_cut: Vec<i32>, mut vertical_cut: Vec<i32>) -> i64 {
horizontal_cut.sort();
vertical_cut.sort();
let (mut i, mut j) = ((m - 2) as isize, (n - 2) as isize);
let (mut h, mut v) = (1_i64, 1_i64);
let mut ans: i64 = 0;

while i >= 0 || j >= 0 {
if j < 0 || (i >= 0 && horizontal_cut[i as usize] > vertical_cut[j as usize]) {
ans += horizontal_cut[i as usize] as i64 * v;
i -= 1;
h += 1;
} else {
ans += vertical_cut[j as usize] as i64 * h;
j -= 1;
v += 1;
}
}

ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,34 @@ function minimumCost(m: number, n: number, horizontalCut: number[], verticalCut:
}
```

#### Rust

```rust
impl Solution {
pub fn minimum_cost(m: i32, n: i32, mut horizontal_cut: Vec<i32>, mut vertical_cut: Vec<i32>) -> i64 {
horizontal_cut.sort();
vertical_cut.sort();
let (mut i, mut j) = ((m - 2) as isize, (n - 2) as isize);
let (mut h, mut v) = (1_i64, 1_i64);
let mut ans: i64 = 0;

while i >= 0 || j >= 0 {
if j < 0 || (i >= 0 && horizontal_cut[i as usize] > vertical_cut[j as usize]) {
ans += horizontal_cut[i as usize] as i64 * v;
i -= 1;
h += 1;
} else {
ans += vertical_cut[j as usize] as i64 * h;
j -= 1;
v += 1;
}
}

ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
impl Solution {
pub fn minimum_cost(m: i32, n: i32, mut horizontal_cut: Vec<i32>, mut vertical_cut: Vec<i32>) -> i64 {
horizontal_cut.sort();
vertical_cut.sort();
let (mut i, mut j) = ((m - 2) as isize, (n - 2) as isize);
let (mut h, mut v) = (1_i64, 1_i64);
let mut ans: i64 = 0;

while i >= 0 || j >= 0 {
if j < 0 || (i >= 0 && horizontal_cut[i as usize] > vertical_cut[j as usize]) {
ans += horizontal_cut[i as usize] as i64 * v;
i -= 1;
h += 1;
} else {
ans += vertical_cut[j as usize] as i64 * h;
j -= 1;
v += 1;
}
}

ans
}
}