您的位置:首页 > 其它

leetcode 26 80 删除已排序数组中重复的数据

2017-03-29 08:58 405 查看
80. Remove Duplicates from Sorted Array II

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.

public static int removeDuplicatesMyself(int[] nums) {

if (nums.length < 2) return nums.length;
final int count = nums.length;
int repeat = 2;
int occor = 0;
int index = 0;
for (int i = 0; i < count; ++i) {
if (occor < repeat) nums[index++] = nums[i];
if (i < count - 1 && nums[i] == nums[i + 1]) {
occor++;
} else {
occor = 0;
}
}
return index;
}


  修改repeat可以实现任意重复次数
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐