diff --git a/solution/3200-3299/3216.Lexicographically Smallest String After a Swap/README.md b/solution/3200-3299/3216.Lexicographically Smallest String After a Swap/README.md index b7bd3ad19f6dc..115279c2abc67 100644 --- a/solution/3200-3299/3216.Lexicographically Smallest String After a Swap/README.md +++ b/solution/3200-3299/3216.Lexicographically Smallest String After a Swap/README.md @@ -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 = 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() + } +} +``` + diff --git a/solution/3200-3299/3216.Lexicographically Smallest String After a Swap/README_EN.md b/solution/3200-3299/3216.Lexicographically Smallest String After a Swap/README_EN.md index de9488609865b..87acfe5b66bbf 100644 --- a/solution/3200-3299/3216.Lexicographically Smallest String After a Swap/README_EN.md +++ b/solution/3200-3299/3216.Lexicographically Smallest String After a Swap/README_EN.md @@ -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 = 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() + } +} +``` + diff --git a/solution/3200-3299/3216.Lexicographically Smallest String After a Swap/Solution.rs b/solution/3200-3299/3216.Lexicographically Smallest String After a Swap/Solution.rs new file mode 100644 index 0000000000000..197fe2fb2db30 --- /dev/null +++ b/solution/3200-3299/3216.Lexicographically Smallest String After a Swap/Solution.rs @@ -0,0 +1,14 @@ +impl Solution { + pub fn get_smallest_string(s: String) -> String { + let mut cs: Vec = 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() + } +} diff --git a/solution/3200-3299/3217.Delete Nodes From Linked List Present in Array/README.md b/solution/3200-3299/3217.Delete Nodes From Linked List Present in Array/README.md index d999985a5ca76..9dbcaae8474c2 100644 --- a/solution/3200-3299/3217.Delete Nodes From Linked List Present in Array/README.md +++ b/solution/3200-3299/3217.Delete Nodes From Linked List Present in Array/README.md @@ -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> +// } +// +// impl ListNode { +// #[inline] +// fn new(val: i32) -> Self { +// ListNode { +// next: None, +// val +// } +// } +// } + +use std::collections::HashSet; + +impl Solution { + pub fn modified_list(nums: Vec, head: Option>) -> Option> { + let s: HashSet = 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 + } +} +``` + diff --git a/solution/3200-3299/3217.Delete Nodes From Linked List Present in Array/README_EN.md b/solution/3200-3299/3217.Delete Nodes From Linked List Present in Array/README_EN.md index 25635df4cc7e0..10f4911d402c6 100644 --- a/solution/3200-3299/3217.Delete Nodes From Linked List Present in Array/README_EN.md +++ b/solution/3200-3299/3217.Delete Nodes From Linked List Present in Array/README_EN.md @@ -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> +// } +// +// impl ListNode { +// #[inline] +// fn new(val: i32) -> Self { +// ListNode { +// next: None, +// val +// } +// } +// } + +use std::collections::HashSet; + +impl Solution { + pub fn modified_list(nums: Vec, head: Option>) -> Option> { + let s: HashSet = 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 + } +} +``` + diff --git a/solution/3200-3299/3217.Delete Nodes From Linked List Present in Array/Solution.rs b/solution/3200-3299/3217.Delete Nodes From Linked List Present in Array/Solution.rs new file mode 100644 index 0000000000000..3da3a3b1bd47b --- /dev/null +++ b/solution/3200-3299/3217.Delete Nodes From Linked List Present in Array/Solution.rs @@ -0,0 +1,36 @@ +// Definition for singly-linked list. +// #[derive(PartialEq, Eq, Clone, Debug)] +// pub struct ListNode { +// pub val: i32, +// pub next: Option> +// } +// +// impl ListNode { +// #[inline] +// fn new(val: i32) -> Self { +// ListNode { +// next: None, +// val +// } +// } +// } + +use std::collections::HashSet; + +impl Solution { + pub fn modified_list(nums: Vec, head: Option>) -> Option> { + let s: HashSet = 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 + } +} diff --git a/solution/3200-3299/3218.Minimum Cost for Cutting Cake I/README.md b/solution/3200-3299/3218.Minimum Cost for Cutting Cake I/README.md index 0be2d066422a0..1f2132c6bb92a 100644 --- a/solution/3200-3299/3218.Minimum Cost for Cutting Cake I/README.md +++ b/solution/3200-3299/3218.Minimum Cost for Cutting Cake I/README.md @@ -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, mut vertical_cut: Vec) -> 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 + } +} +``` + diff --git a/solution/3200-3299/3218.Minimum Cost for Cutting Cake I/README_EN.md b/solution/3200-3299/3218.Minimum Cost for Cutting Cake I/README_EN.md index 9d77bdd991469..e6a6adb654a48 100644 --- a/solution/3200-3299/3218.Minimum Cost for Cutting Cake I/README_EN.md +++ b/solution/3200-3299/3218.Minimum Cost for Cutting Cake I/README_EN.md @@ -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, mut vertical_cut: Vec) -> 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 + } +} +``` + diff --git a/solution/3200-3299/3218.Minimum Cost for Cutting Cake I/Solution.rs b/solution/3200-3299/3218.Minimum Cost for Cutting Cake I/Solution.rs new file mode 100644 index 0000000000000..f6943ca3dfde8 --- /dev/null +++ b/solution/3200-3299/3218.Minimum Cost for Cutting Cake I/Solution.rs @@ -0,0 +1,23 @@ +impl Solution { + pub fn minimum_cost(m: i32, n: i32, mut horizontal_cut: Vec, mut vertical_cut: Vec) -> 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 + } +} diff --git a/solution/3200-3299/3219.Minimum Cost for Cutting Cake II/README.md b/solution/3200-3299/3219.Minimum Cost for Cutting Cake II/README.md index 14d3bfb3b2c6e..1215985c3a2ea 100644 --- a/solution/3200-3299/3219.Minimum Cost for Cutting Cake II/README.md +++ b/solution/3200-3299/3219.Minimum Cost for Cutting Cake II/README.md @@ -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, mut vertical_cut: Vec) -> 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 + } +} +``` + diff --git a/solution/3200-3299/3219.Minimum Cost for Cutting Cake II/README_EN.md b/solution/3200-3299/3219.Minimum Cost for Cutting Cake II/README_EN.md index 502af18615bb1..774b4bf6b0292 100644 --- a/solution/3200-3299/3219.Minimum Cost for Cutting Cake II/README_EN.md +++ b/solution/3200-3299/3219.Minimum Cost for Cutting Cake II/README_EN.md @@ -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, mut vertical_cut: Vec) -> 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 + } +} +``` + diff --git a/solution/3200-3299/3219.Minimum Cost for Cutting Cake II/Solution.rs b/solution/3200-3299/3219.Minimum Cost for Cutting Cake II/Solution.rs new file mode 100644 index 0000000000000..e0253399a77b6 --- /dev/null +++ b/solution/3200-3299/3219.Minimum Cost for Cutting Cake II/Solution.rs @@ -0,0 +1,23 @@ +impl Solution { + pub fn minimum_cost(m: i32, n: i32, mut horizontal_cut: Vec, mut vertical_cut: Vec) -> 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 + } +}

AltStyle によって変換されたページ (->オリジナル) /