Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit eebfbfc

Browse files
author
guangsheng.li01
committed
Solved 0026
1 parent 63cd7d8 commit eebfbfc

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* [0026] remove-duplicates-from-sorted-array
3+
*/
4+
5+
pub struct Solution {}
6+
7+
// solution impl starts here
8+
9+
impl Solution {
10+
pub fn remove_duplicates(nums: &mut Vec<i32>) -> i32 {
11+
let mut i = 1;
12+
while i < nums.len() {
13+
if nums[i - 1] == nums[i] {
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![1, 1, 2];
34+
assert_eq!(Solution::remove_duplicates(&mut nums), 2);
35+
assert_eq!(nums, vec![1, 2]);
36+
}
37+
38+
#[test]
39+
fn test_case1() {
40+
let mut nums = vec![0, 0, 1, 1, 1, 2, 2, 3, 3, 4];
41+
assert_eq!(Solution::remove_duplicates(&mut nums), 5);
42+
assert_eq!(nums, vec![0, 1, 2, 3, 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
@@ -7,6 +7,7 @@ mod a0000_sample;
77
mod a0001_two_sum;
88
mod a0007_reverse_integer;
99
mod a0009_palindrome_number;
10+
mod a0026_remove_duplicates_from_sorted_array;
1011
mod a0145_binary_tree_postorder_traversal;
1112
mod a0172_factorial_trailing_zeroes;
1213
mod a0400_nth_digit;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /