-
Notifications
You must be signed in to change notification settings - Fork 0
/
s0027_remove_element.rs
35 lines (32 loc) · 938 Bytes
/
s0027_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
struct Solution;
//leetcode submit region begin(Prohibit modification and deletion)
impl Solution {
pub fn remove_element(nums: &mut Vec<i32>, val: i32) -> i32 {
let mut slow = 0;
let mut fast = 0;
while fast < nums.len() {
if nums[fast] != val {
nums[slow] = nums[fast];
slow += 1;
}
fast += 1;
}
slow as i32
}
}
//leetcode submit region end(Prohibit modification and deletion)
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test() {
let mut nums = vec![3, 2, 2, 3];
let val = 3;
assert_eq!(Solution::remove_element(&mut nums, val), 2);
assert_eq!(nums[..2], vec![2, 2]);
let mut nums = vec![0, 1, 2, 2, 3, 0, 4, 2];
let val = 2;
assert_eq!(Solution::remove_element(&mut nums, val), 5);
assert_eq!(nums[..5], vec![0, 1, 3, 0, 4]);
}
}