Skip to content

Commit 703e269

Browse files
committed
feat: add rust solution to lc problem: No.0905
No.0905.Sort Array By Parity
1 parent 61b3c99 commit 703e269

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

solution/0900-0999/0905.Sort Array By Parity/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,26 @@ var sortArrayByParity = function (A) {
107107
};
108108
```
109109

110+
### **Rust**
111+
112+
```rust
113+
impl Solution {
114+
pub fn sort_array_by_parity(mut nums: Vec<i32>) -> Vec<i32> {
115+
let (mut l, mut r) = (0, nums.len() - 1);
116+
while l < r {
117+
while l < r && nums[l] & 1 == 0 {
118+
l += 1;
119+
}
120+
while l < r && nums[r] & 1 == 1 {
121+
r -= 1;
122+
}
123+
nums.swap(l, r);
124+
}
125+
nums
126+
}
127+
}
128+
```
129+
110130
### **...**
111131

112132
```

solution/0900-0999/0905.Sort Array By Parity/README_EN.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,26 @@ var sortArrayByParity = function (A) {
103103
};
104104
```
105105

106+
### **Rust**
107+
108+
```rust
109+
impl Solution {
110+
pub fn sort_array_by_parity(mut nums: Vec<i32>) -> Vec<i32> {
111+
let (mut l, mut r) = (0, nums.len() - 1);
112+
while l < r {
113+
while l < r && nums[l] & 1 == 0 {
114+
l += 1;
115+
}
116+
while l < r && nums[r] & 1 == 1 {
117+
r -= 1;
118+
}
119+
nums.swap(l, r);
120+
}
121+
nums
122+
}
123+
}
124+
```
125+
106126
### **...**
107127

108128
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
impl Solution {
2+
pub fn sort_array_by_parity(mut nums: Vec<i32>) -> Vec<i32> {
3+
let (mut l, mut r) = (0, nums.len() - 1);
4+
while l < r {
5+
while l < r && nums[l] & 1 == 0 {
6+
l += 1;
7+
}
8+
while l < r && nums[r] & 1 == 1 {
9+
r -= 1;
10+
}
11+
nums.swap(l, r);
12+
}
13+
nums
14+
}
15+
}

0 commit comments

Comments
 (0)