Remove Duplicates from Sorted Array
思路一: 双指针,左指针永远指向有效数组长度+1的位置,左指针只会在出现交换后向右移动。右指针一直向右扫描,遇到不重复的数字就和左指针交换。
public int removeDuplicates(int[] nums) {
int left = 1;
int right = 1;
int pre = nums[0];
while (right < nums.length) {
if (nums[right] != pre) {
pre = nums[right];
nums[left] = nums[right];
left++;
}
right++;
}
return left;
}
标签:pre,26,right,nums,int,easy,指针,leetcode,left
From: https://www.cnblogs.com/iyiluo/p/16780088.html