-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRemoveDupSortArr.java
More file actions
52 lines (35 loc) · 1005 Bytes
/
Copy pathRemoveDupSortArr.java
File metadata and controls
52 lines (35 loc) · 1005 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.Arrays;
/*
** leetcode : https://leetcode.com/problems/remove-duplicates-from-sorted-array/
*/
public class RemoveDupSortArr{
public static void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
public static int removeDuplicates(int[] nums) {
if (nums.length == 1) {
return 1;
}
int pivot = 0;
int i = 1, j;
int count;
while (i < nums.length) {
j = i;
while (j < nums.length && nums[j] <= nums[pivot]){
j++;
}
if (j < nums.length) {
pivot++;
swap(nums, pivot, j);
}
i = j + 1;
}
return pivot + 1;
}
public static void main(String[] args) {
assert removeDuplicates(new int[]{1 , 1, 2}) == 2;
assert removeDuplicates(new int[]{0, 0, 1, 1, 1, 2, 2, 3, 3, 4}) == 5;
}
}