26. 删除有序数组中的重复项 #
class Solution {
public int removeDuplicates(int[] nums) {
int left = 0, n = nums.length;
for(int right = 1; right < n; right++) {
if(nums[left] != nums[right]) {
nums[++left] = nums[right];
}
}
return left+1;
}
}
80. 删除有序数组中的重复项 II #
class Solution {
public int removeDuplicates(int[] nums) {
int n = nums.length;
if (n <= 2) {
return n;
}
int slow = 0, fast = 2;
while (fast < n) {
if (nums[slow] != nums[fast]) {
nums[slow+2] = nums[fast];
++slow;
}
++fast;
}
return slow+2;
}
}