题目
代码
class Solution {
public int removeDuplicates(int[] nums) {
int index = 1;
for(int i=1;i<nums.length;i++) {
if(nums[i]!=nums[i-1]) {
nums[index++] = nums[i];
}
}return index;
}
}
class Solution {
public int removeDuplicates(int[] nums) {
int i = 0,j = 0;
while(j<nums.length){
if(nums[i]==nums[j]) j++;
else {
nums[i+1]=nums[j];
i++;
j++;
}
}return i+1;
}
}
class Solution {
public int removeDuplicates(int[] nums) {
int i = 0;
for(int j =0;j<nums.length;j++) {
if(j==0||nums[j]!=nums[j-1]) {
nums[i++] = nums[j];
}
}return i;
}
}
结果