|
| 1 | +/** |
| 2 | + * [27] Remove Element |
| 3 | + * |
| 4 | + * Given an array nums and a value val, remove all instances of that value <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> and return the new length. |
| 5 | + * |
| 6 | + * Do not allocate extra space for another array, you must do this by modifying the input array <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> with O(1) extra memory. |
| 7 | + * |
| 8 | + * The order of elements can be changed. It doesn't matter what you leave beyond the new length. |
| 9 | + * |
| 10 | + * Example 1: |
| 11 | + * |
| 12 | + * |
| 13 | + * Given nums = [3,2,2,3], val = 3, |
| 14 | + * |
| 15 | + * Your function should return length = 2, with the first two elements of nums being 2. |
| 16 | + * |
| 17 | + * It doesn't matter what you leave beyond the returned length. |
| 18 | + * |
| 19 | + * |
| 20 | + * Example 2: |
| 21 | + * |
| 22 | + * |
| 23 | + * Given nums = [0,1,2,2,3,0,4,2], val = 2, |
| 24 | + * |
| 25 | + * Your function should return length = 5, with the first five elements of nums containing 0, 1, 3, 0, and 4. |
| 26 | + * |
| 27 | + * Note that the order of those five elements can be arbitrary. |
| 28 | + * |
| 29 | + * It doesn't matter what values are set beyond the returned length. |
| 30 | + * |
| 31 | + * Clarification: |
| 32 | + * |
| 33 | + * Confused why the returned value is an integer but your answer is an array? |
| 34 | + * |
| 35 | + * Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well. |
| 36 | + * |
| 37 | + * Internally you can think of this: |
| 38 | + * |
| 39 | + * |
| 40 | + * // nums is passed in by reference. (i.e., without making a copy) |
| 41 | + * int len = removeElement(nums, val); |
| 42 | + * |
| 43 | + * // any modification to nums in your function would be known by the caller. |
| 44 | + * // using the length returned by your function, it prints the first len elements. |
| 45 | + * for (int i = 0; i < len; i++) { |
| 46 | + * print(nums[i]); |
| 47 | + * } |
| 48 | + * |
| 49 | + */ |
| 50 | +pub struct Solution {} |
| 51 | + |
| 52 | +// submission codes start here |
| 53 | + |
| 54 | +impl Solution { |
| 55 | + pub fn remove_element(nums: &mut Vec<i32>, val: i32) -> i32 { |
| 56 | + if nums.len() < 1 { return 0 } |
| 57 | + let (mut start, mut end) = (0_usize, nums.len()-1); |
| 58 | + while start < end { |
| 59 | + if nums[start] == val { |
| 60 | + nums[start] = nums[end-1]; |
| 61 | + end -= 1; |
| 62 | + } else { |
| 63 | + start += 1; |
| 64 | + } |
| 65 | + } |
| 66 | + end as i32 |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// submission codes end |
| 71 | + |
| 72 | +#[cfg(test)] |
| 73 | +mod tests { |
| 74 | + use super::*; |
| 75 | + |
| 76 | + #[test] |
| 77 | + fn test_27() { |
| 78 | + let mut vec1 = vec![0,1,2,2,3,0,4,2]; |
| 79 | + assert_eq!(Solution::remove_element(&mut vec1, 2), 5); |
| 80 | + assert_eq!(vec1[0..5], [0,1,4,0,3]); |
| 81 | + assert_eq!(Solution::remove_element(&mut vec![], 2), 0); |
| 82 | + assert_eq!(Solution::remove_element(&mut vec![1,2,2,2,2,2,2], 2), 1); |
| 83 | + assert_eq!(Solution::remove_element(&mut vec![2,2,2,2,2,2,2], 2), 0); |
| 84 | + assert_eq!(Solution::remove_element(&mut vec![1], 1), 0); |
| 85 | + } |
| 86 | +} |
0 commit comments