/*
* @Author: sumBorn
* @Date: 2022-02-21 16:13:52
* @LastEditTime: 2022-02-22 15:00:58
* @Description: https://leetcode-cn.com/leetbook/read/all-about-array/x9a60t/
*/
/**
* @description: 双指针,以右侧遍历
* @param {*}
* @return {*}
*/
public class Solution
{
public int RemoveDuplicates(int[] nums)
{
if (nums.Length <= 1) return nums.Length;
int left = 0;
for (var right = 1; right < nums.Length; right++)
{
if (nums[right] != nums[left])
{
left++;
nums[left] = nums[right];
}
}
return ++left;
}
}
/**
* @description: old
* @param {*}
* @return {*}
*/
public class Solution
{
public int RemoveDuplicates(int[] nums)
{
if (nums.Length == 0) return 0;
int length = 1;
int j = 1;
for (var i = 0; i < nums.Length && j < nums.Length;)
{
if (nums[i] == nums[j])
{
j++;
}
else
{
i++;
nums[i] = nums[j];
j++;
length++;
}
}
return length;
}
}
网友评论