题目链接:https://leetcode.com/problems/remove-duplicates-from-sorted-array/
题目:
once
Do not allocate extra space for another array, you must do this in place with constant memory.
For example,
Given input array nums = [1,1,2]
,2
, with the first two elements of nums being 1
and 2
思路:
思路跟 一样。
算法:
public int removeDuplicates(int[] nums) {
int count = 0;
for (int i = 1; i < nums.length; i++) {
if (nums[i] == nums[i -1]) {
count++;
}
nums[i - count] = nums[i];
}
return nums.length - count;
}