-
Notifications
You must be signed in to change notification settings - Fork 83
/
RemoveDuplicatesFromSortedArray26.java
59 lines (54 loc) · 1.53 KB
/
RemoveDuplicatesFromSortedArray26.java
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
53
54
55
56
57
58
59
/**
* Given a sorted array, remove the duplicates in place such that each element
* appear only once and return the new length.
*
* Do not allocate extra space for another array, you must do this in place with
* constant memory.
*
* For example,
* Given input array nums = [1,1,2],
*
* Your function should return length = 2, with the first two elements of nums
* being 1 and 2 respectively. It doesn't matter what you leave beyond the new
* length.
*
*/
public class RemoveDuplicatesFromSortedArray26 {
public int removeDuplicates(int[] nums) {
if (nums.length <= 1) {
return nums.length;
}
int slow = 0;
int fast = 1;
while (fast < nums.length) {
if (nums[fast] != nums[slow]) {
slow++;
nums[slow] = nums[fast];
}
fast++;
}
return slow+1;
}
/**
* https://discuss.leetcode.com/topic/17252/5-lines-c-java-nicer-loops
*/
public int removeDuplicates2(int[] nums) {
int i = nums.length > 0 ? 1 : 0;
for (int n : nums)
if (n > nums[i-1])
nums[i++] = n;
return i;
}
public int removeDuplicates3(int[] nums) {
if (nums == null || nums.length == 0) return 0;
int len = nums.length;
int i = 0;
int j = 0;
while (j < len) {
while (j > 0 && j < len && nums[j] == nums[j-1]) j++;
if (j == len) break;
nums[i++] = nums[j++];
}
return i;
}
}