作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array-ii/solution/shan-chu-pai-xu-shu-zu-zhong-de-zhong-fu-yec2/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
个人微改:
class remove{
public int removeDuplicates(int[] nums) {
int n = nums.length;
if(n<=2) return n;
int slow = 2,fast = 2;
while(fast<n) {
if(nums[slow-2]!=nums[fast]) {
nums[slow] = nums[fast];
slow++;
}
fast++;
}
return slow;
}
}
public class RemoveDuplicatesFromSortedArrayII {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] nums = new int[] {1,1,1,2,2,3};
int[] nums2 = new int[] {0,0,1,1,1,1,2,3,3};
remove hah = new remove();
System.out.println(hah.removeDuplicates(nums));
System.out.println(hah.removeDuplicates(nums2));
}
}

953

被折叠的 条评论
为什么被折叠?



