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 by modifying the input array in-place with O(1) extra memory.
class Solution {
public int removeDuplicates(int[] nums) {
if (nums.length==0 || nums.length==1) {
return nums.length;
}
int tmpLen = 0;
for (int j=1; j<nums.length; j++) {
if (nums[tmpLen] != nums[j]) {
tmpLen++;
nums[tmpLen] = nums[j];
}
}
return ++tmpLen;
}
}
已提交版本,Accepted。尤其要注意最后的++tmpLen的返回先后顺序。