Skip to content

Commit 3dac3f3

Browse files
authored
feat: add rust solution to lc problem: No.2460 (#1345)
Signed-off-by: xiaolatiao <1628652790@qq.com>
1 parent f516265 commit 3dac3f3

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

solution/2400-2499/2460.Apply Operations to an Array/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,33 @@ function applyOperations(nums: number[]): number[] {
192192
}
193193
```
194194

195+
### **Rust**
196+
197+
```rust
198+
impl Solution {
199+
pub fn apply_operations(nums: Vec<i32>) -> Vec<i32> {
200+
let mut nums = nums;
201+
202+
for i in 0..nums.len() - 1 {
203+
if nums[i] == nums[i + 1] {
204+
nums[i] <<= 1;
205+
nums[i + 1] = 0;
206+
}
207+
}
208+
209+
let mut cur = 0;
210+
for i in 0..nums.len() {
211+
if nums[i] != 0 {
212+
nums.swap(i, cur);
213+
cur += 1;
214+
}
215+
}
216+
217+
nums
218+
}
219+
}
220+
```
221+
195222
### **...**
196223

197224
```

solution/2400-2499/2460.Apply Operations to an Array/README_EN.md

+27
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,33 @@ function applyOperations(nums: number[]): number[] {
170170
}
171171
```
172172

173+
### **Rust**
174+
175+
```rust
176+
impl Solution {
177+
pub fn apply_operations(nums: Vec<i32>) -> Vec<i32> {
178+
let mut nums = nums;
179+
180+
for i in 0..nums.len() - 1 {
181+
if nums[i] == nums[i + 1] {
182+
nums[i] <<= 1;
183+
nums[i + 1] = 0;
184+
}
185+
}
186+
187+
let mut cur = 0;
188+
for i in 0..nums.len() {
189+
if nums[i] != 0 {
190+
nums.swap(i, cur);
191+
cur += 1;
192+
}
193+
}
194+
195+
nums
196+
}
197+
}
198+
```
199+
173200
### **...**
174201

175202
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
impl Solution {
2+
pub fn apply_operations(nums: Vec<i32>) -> Vec<i32> {
3+
let mut nums = nums;
4+
5+
for i in 0..nums.len() - 1 {
6+
if nums[i] == nums[i + 1] {
7+
nums[i] <<= 1;
8+
nums[i + 1] = 0;
9+
}
10+
}
11+
12+
let mut cur = 0;
13+
for i in 0..nums.len() {
14+
if nums[i] != 0 {
15+
nums.swap(i, cur);
16+
cur += 1;
17+
}
18+
}
19+
20+
nums
21+
}
22+
}

0 commit comments

Comments
 (0)