-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove-element.rs
57 lines (50 loc) · 1.3 KB
/
remove-element.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#![allow(unused)]
/// https://leetcode-cn.com/problems/remove-element/
struct Solution;
impl Solution {
fn impl_1(nums: &mut Vec<i32>, val: i32) -> i32 {
let mut idx = 0;
while idx < nums.len() {
if nums[idx] == val {
nums.remove(idx);
} else {
idx += 1;
}
}
idx as i32
}
fn impl_2(nums: &mut [i32], val: i32) -> i32 {
let mut idx = 0;
let len = nums.len();
for i in 0..len {
if nums[i] != val {
nums[idx] = nums[i];
idx += 1;
}
}
idx as i32
}
pub fn remove_element(nums: &mut [i32], val: i32) -> i32 {
// Solution::impl_1(nums, val)
Solution::impl_2(nums, val)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn impl_1() {
let s = Solution::impl_1(&mut vec![0, 1, 2, 2, 3, 0, 4, 2], 2);
assert_eq!(s, 5);
let s = Solution::impl_1(&mut vec![0, 1, 2, 2, 2, 3, 0, 4, 2], 2);
assert_eq!(s, 5);
}
#[test]
fn impl_2() {
let s = Solution::impl_2(&mut [0, 1, 2, 2, 3, 0, 4, 2], 2);
assert_eq!(s, 5);
let s = Solution::impl_2(&mut [0, 1, 2, 2, 2, 3, 0, 4, 2], 2);
assert_eq!(s, 5);
}
}
fn main() {}