20240403 Finished

This commit is contained in:
jackfiled 2024-04-03 10:10:05 +08:00
parent 174a17f94c
commit 507dcb4815
2 changed files with 54 additions and 1 deletions

View File

@ -92,3 +92,4 @@ mod p2908_minimum_sum_of_mountain_triplets_i;
mod p2952_minimum_number_of_coins_to_be_added; mod p2952_minimum_number_of_coins_to_be_added;
mod p331_verify_preorder_serialization_of_a_binary_tree; mod p331_verify_preorder_serialization_of_a_binary_tree;
mod p88_merge_sorted_array; mod p88_merge_sorted_array;
mod p27_remove_element;

View File

@ -0,0 +1,52 @@
/**
* [27] Remove Element
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn remove_element(nums: &mut Vec<i32>, val: i32) -> i32 {
if nums.len() == 0 {
return 0;
}
let mut last = nums.len() - 1;
let mut i = 0;
while i <= last {
while nums[i] == val {
if i == last {
return last as i32;
}
nums[i] = nums[last];
last -= 1;
}
i += 1;
}
(last + 1) as i32
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_27() {
let mut array = vec![3,2,2,3];
assert_eq!(2, Solution::remove_element(&mut array, 3));
for i in 0..2 {
assert_eq!(2, array[i]);
}
let mut array = vec![3];
assert_eq!(0, Solution::remove_element(&mut array, 3));
}
}