Merge branch 'master' of git.rrricardo.top:jackfiled/leetcode
This commit is contained in:
commit
464e7130e8
|
@ -92,4 +92,5 @@ mod p2908_minimum_sum_of_mountain_triplets_i;
|
|||
mod p2952_minimum_number_of_coins_to_be_added;
|
||||
mod p331_verify_preorder_serialization_of_a_binary_tree;
|
||||
mod p88_merge_sorted_array;
|
||||
mod p26_remove_duplicates_from_sorted_array;
|
||||
mod p26_remove_duplicates_from_sorted_array;
|
||||
mod p27_remove_element;
|
52
src/problem/p27_remove_element.rs
Normal file
52
src/problem/p27_remove_element.rs
Normal 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));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user