原题链接在这里:https://leetcode.com/problems/remove-duplicates-from-sorted-array/
利用counter来更改新array前面的元素,因为原题中知名length后面的元素没有关系。
AC Java:
public class Solution {
public int removeDuplicates(int[] nums) {
if(nums == null || nums.length <1)
return 0;
int counter = 1;
for(int i = 1;i<nums.length;i++){
if(nums[i] != nums[i-1]){
nums[counter] = nums[i];
counter++;
}
}
return counter;
}
}
进阶题目Remove Duplicates from Sorted Array II
可以同时看下Remove Duplicates from Sorted List这道题目。