forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_26.java
43 lines (38 loc) · 1.13 KB
/
_26.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
package com.fishercoder.solutions;
import com.fishercoder.common.utils.CommonUtils;
public class _26 {
public static class Solution1 {
/**
* Key: It doesn't matter what you leave beyond the returned length.
*/
public int removeDuplicates(int[] nums) {
int i = 0;
for (int j = 1; j < nums.length; j++) {
if (nums[i] != nums[j]) {
i++;
nums[i] = nums[j];
}
}
return i + 1;
}
}
public static class Solution2 {
/**
* My completely original solution on 2/2/2022.
*/
public int removeDuplicates(int[] nums) {
int left = 0;
int right = 1;
for (; right < nums.length; right++) {
while (right < nums.length && nums[right] == nums[right - 1]) {
right++;
}
if (right < nums.length) {
nums[++left] = nums[right];
}
}
CommonUtils.printArray(nums);
return left + 1;
}
}
}