Skip to content

Commit 576675f

Browse files
committed
feat: add rust solutions to lc problems
No.0026,0027,0080,0169,0283
1 parent d15926b commit 576675f

File tree

15 files changed

+245
-0
lines changed

15 files changed

+245
-0
lines changed

solution/0000-0099/0026.Remove Duplicates from Sorted Array/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,23 @@ public class Solution {
171171
}
172172
```
173173

174+
### **Rust**
175+
176+
```rs
177+
impl Solution {
178+
pub fn remove_duplicates(nums: &mut Vec<i32>) -> i32 {
179+
let mut len = 0;
180+
for i in 0..nums.len() {
181+
if i == 0 || nums[i] != nums[len - 1] {
182+
nums[len] = nums[i];
183+
len += 1;
184+
}
185+
}
186+
len as i32
187+
}
188+
}
189+
```
190+
174191
### **...**
175192

176193
```

solution/0000-0099/0026.Remove Duplicates from Sorted Array/README_EN.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,23 @@ public class Solution {
151151
}
152152
```
153153

154+
### **Rust**
155+
156+
```rs
157+
impl Solution {
158+
pub fn remove_duplicates(nums: &mut Vec<i32>) -> i32 {
159+
let mut len = 0;
160+
for i in 0..nums.len() {
161+
if i == 0 || nums[i] != nums[len - 1] {
162+
nums[len] = nums[i];
163+
len += 1;
164+
}
165+
}
166+
len as i32
167+
}
168+
}
169+
```
170+
154171
### **...**
155172

156173
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
impl Solution {
2+
pub fn remove_duplicates(nums: &mut Vec<i32>) -> i32 {
3+
let mut len = 0;
4+
for i in 0..nums.len() {
5+
if i == 0 || nums[i] != nums[len - 1] {
6+
nums[len] = nums[i];
7+
len += 1;
8+
}
9+
}
10+
len as i32
11+
}
12+
}

solution/0000-0099/0027.Remove Element/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,23 @@ func removeElement(nums []int, val int) int {
151151
}
152152
```
153153

154+
### **Rust**
155+
156+
```rs
157+
impl Solution {
158+
pub fn remove_element(nums: &mut Vec<i32>, val: i32) -> i32 {
159+
let mut len = 0;
160+
for i in 0..nums.len() {
161+
if nums[i] != val {
162+
nums[len] = nums[i];
163+
len += 1;
164+
}
165+
}
166+
len as i32
167+
}
168+
}
169+
```
170+
154171
### **...**
155172

156173
```

solution/0000-0099/0027.Remove Element/README_EN.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,23 @@ func removeElement(nums []int, val int) int {
139139
}
140140
```
141141

142+
### **Rust**
143+
144+
```rs
145+
impl Solution {
146+
pub fn remove_element(nums: &mut Vec<i32>, val: i32) -> i32 {
147+
let mut len = 0;
148+
for i in 0..nums.len() {
149+
if nums[i] != val {
150+
nums[len] = nums[i];
151+
len += 1;
152+
}
153+
}
154+
len as i32
155+
}
156+
}
157+
```
158+
142159
### **...**
143160

144161
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
impl Solution {
2+
pub fn remove_element(nums: &mut Vec<i32>, val: i32) -> i32 {
3+
let mut len = 0;
4+
for i in 0..nums.len() {
5+
if nums[i] != val {
6+
nums[len] = nums[i];
7+
len += 1;
8+
}
9+
}
10+
len as i32
11+
}
12+
}

solution/0000-0099/0080.Remove Duplicates from Sorted Array II/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,23 @@ func removeDuplicates(nums []int) int {
171171
}
172172
```
173173

174+
### **Rust**
175+
176+
```rs
177+
impl Solution {
178+
pub fn remove_duplicates(nums: &mut Vec<i32>) -> i32 {
179+
let mut len = 0;
180+
for i in 0..nums.len() {
181+
if i < 2 || nums[i] != nums[len - 2] {
182+
nums[len] = nums[i];
183+
len += 1;
184+
}
185+
}
186+
len as i32
187+
}
188+
}
189+
```
190+
174191
### **...**
175192

176193
```

solution/0000-0099/0080.Remove Duplicates from Sorted Array II/README_EN.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,23 @@ func removeDuplicates(nums []int) int {
152152
}
153153
```
154154

155+
### **Rust**
156+
157+
```rs
158+
impl Solution {
159+
pub fn remove_duplicates(nums: &mut Vec<i32>) -> i32 {
160+
let mut len = 0;
161+
for i in 0..nums.len() {
162+
if i < 2 || nums[i] != nums[len - 2] {
163+
nums[len] = nums[i];
164+
len += 1;
165+
}
166+
}
167+
len as i32
168+
}
169+
}
170+
```
171+
155172
### **...**
156173

157174
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
impl Solution {
2+
pub fn remove_duplicates(nums: &mut Vec<i32>) -> i32 {
3+
let mut len = 0;
4+
for i in 0..nums.len() {
5+
if i < 2 || nums[i] != nums[len - 2] {
6+
nums[len] = nums[i];
7+
len += 1;
8+
}
9+
}
10+
len as i32
11+
}
12+
}

solution/0100-0199/0169.Majority Element/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,26 @@ func majorityElement(nums []int) int {
165165
}
166166
```
167167

168+
### **Rust**
169+
170+
```rs
171+
impl Solution {
172+
pub fn majority_element(nums: Vec<i32>) -> i32 {
173+
let mut major = 0;
174+
let mut cnt = 0;
175+
for &num in nums.iter() {
176+
if cnt == 0 {
177+
major = num;
178+
cnt = 1;
179+
} else {
180+
cnt += if major == num { 1 } else { -1 };
181+
}
182+
}
183+
major
184+
}
185+
}
186+
```
187+
168188
### **...**
169189

170190
```

0 commit comments

Comments
 (0)