标签:col color discuss sorted 代码 pre tps ruby for
Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?
For example,
Given sorted array A = [1,1,1,2,2,3]
,
Your function should return length = 5
, and A is now [1,1,2,2,3]
.
有序数组,如果要删除值,必须当前位置的值 > 当前位置-2的值.
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] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)
[LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)
标签:col color discuss sorted 代码 pre tps ruby for
原文地址:https://www.cnblogs.com/fanguangdexiaoyuer/p/10777411.html