LeetCode 80. Remove Duplicates from Sorted Array II
Description
Follow up for “Remove Duplicates”:
What if duplicates are allowed at most twice?
For example,
Given sorted array nums = [1,1,1,2,2,3],
Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn’t matter what you leave beyond the new length.
class Solution {
public int removeDuplicates(int[] nums) {
int i = 0;
for (int n: nums) {
if (i < 2 || n > nums[i - 2])
nums[i++] = n;
}
return i;
}
}
本文介绍了解决LeetCode 80题的一种方法,该题要求将一个已排序的数组中重复出现超过两次的元素删除,并返回处理后的数组长度。示例:对于输入数组[1,1,1,2,2,3],经过处理后应当返回长度为5的数组,元素为[1,1,2,2,3]。
743

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



