问题
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.
代码
public int removeDuplicates(int[] nums) { int i = 0; for(int n : nums) { if(i==0||n>nums[i-1]) nums[i++] = n; } return i; }
因为是有序的数组所以可以通过foreach循环直接进行比较。