20240405 Finished
This commit is contained in:
parent
a114d636e7
commit
4a257c4076
|
@ -94,3 +94,4 @@ mod p331_verify_preorder_serialization_of_a_binary_tree;
|
|||
mod p88_merge_sorted_array;
|
||||
mod p26_remove_duplicates_from_sorted_array;
|
||||
mod p27_remove_element;
|
||||
mod p80_remove_duplicates_from_sorted_array_ii;
|
45
src/problem/p80_remove_duplicates_from_sorted_array_ii.rs
Normal file
45
src/problem/p80_remove_duplicates_from_sorted_array_ii.rs
Normal file
|
@ -0,0 +1,45 @@
|
|||
/**
|
||||
* [80] Remove Duplicates from Sorted Array II
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
|
||||
// submission codes start here
|
||||
|
||||
impl Solution {
|
||||
pub fn remove_duplicates(nums: &mut Vec<i32>) -> i32 {
|
||||
let mut now = 1;
|
||||
let mut count = 1;
|
||||
|
||||
for i in 1..nums.len() {
|
||||
if nums[i - 1] != nums[i] {
|
||||
count = 1;
|
||||
nums[now] = nums[i];
|
||||
now += 1;
|
||||
} else {
|
||||
if count < 2 {
|
||||
nums[now] = nums[i];
|
||||
now += 1;
|
||||
}
|
||||
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
now as i32
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_80() {
|
||||
let mut nums = vec![1,1,1,2,2,3];
|
||||
assert_eq!(5, Solution::remove_duplicates(&mut nums));
|
||||
assert_eq!(vec![1,1,2,2,3,3], nums);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user