I try to use dictionary ,but failed.
原题链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array-ii/
public static int RemoveDuplicates(int[] nums)
{
int i = 0;
foreach (var n in nums.Distinct())
{
int j = nums.Count(x => x == n);
j = j > 2 ? 2 : j;
while (j>0)
{
nums[i] = n;
j--;
i++;
}
}
return i;
}
另一种方法:
public static int RemoveDuplicates(int[] nums)
{
int i = 0;
foreach (var n in nums)
{
if (i < 2 || n > nums[i - 2])
nums[i++] = n;
}
return i;
}