Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 4b45088

Browse files
author
guangsheng.li01
committed
Solved 0027
1 parent eebfbfc commit 4b45088

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

src/a0027_remove_element.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* [0027] remove-element
3+
*/
4+
5+
pub struct Solution {}
6+
7+
// solution impl starts here
8+
9+
impl Solution {
10+
pub fn remove_element(nums: &mut Vec<i32>, val: i32) -> i32 {
11+
let mut i = 0;
12+
while i < nums.len() {
13+
if nums[i] == val {
14+
nums.remove(i);
15+
} else {
16+
i += 1;
17+
}
18+
}
19+
nums.len() as i32
20+
}
21+
}
22+
23+
// solution impl ends here
24+
25+
// solution tests starts here
26+
27+
#[cfg(test)]
28+
mod tests {
29+
use super::*;
30+
31+
#[test]
32+
fn test_case0() {
33+
let mut nums = vec![3, 2, 2, 3];
34+
assert_eq!(Solution::remove_element(&mut nums, 2), 2);
35+
assert_eq!(nums, vec![3, 3]);
36+
}
37+
38+
#[test]
39+
fn test_case1() {
40+
let mut nums = vec![0, 1, 2, 2, 3, 0, 4, 2];
41+
assert_eq!(Solution::remove_element(&mut nums, 2), 5);
42+
assert_eq!(nums, vec![0, 1, 3, 0, 4]);
43+
}
44+
}
45+
46+
// solution tests ends here

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ mod a0001_two_sum;
88
mod a0007_reverse_integer;
99
mod a0009_palindrome_number;
1010
mod a0026_remove_duplicates_from_sorted_array;
11+
mod a0027_remove_element;
1112
mod a0145_binary_tree_postorder_traversal;
1213
mod a0172_factorial_trailing_zeroes;
1314
mod a0400_nth_digit;

0 commit comments

Comments
 (0)