关闭

[LeetCode 80]Remove Duplicates from Sorted Array II

513人阅读 评论(0) 收藏 举报
分类:

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 1122 and 3. It doesn't matter what you leave beyond the new length.

Subscribe to see which companies asked this question

C++实现: 该版本拓展性较好,将index和i的初始值改成n,index-2改成index-n 即可拓展成保留N位的版本.

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        if (nums.size()<3)
        return nums.size();
        int index=2;
        for(int i=2;i<nums.size();i++)
        {
            if(nums[index-2]!=nums[i])
            nums[index++]=nums[i];
        }
        return index;
    }
};


0
0
查看评论

Remove Duplicates from Sorted Array II -- LeetCode

原题链接: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/  这道题跟Remove Duplicates from Sorted Array比较类似,区别只是这里元素可以重复出现...
  • linhuanmars
  • linhuanmars
  • 2014-04-23 08:39
  • 10683

[LeetCode]80.Remove Duplicates from Sorted Array II

【题目】 Remove Duplicates from Sorted Array II  Total Accepted: 4460 Total Submissions: 15040My Submissions Follow up for &qu...
  • SunnyYoona
  • SunnyYoona
  • 2014-01-15 09:51
  • 1749

Remove Duplicates from Sorted Array -- LeetCode

原题链接: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/  这道题跟Remove Element类似,也是考察数组的基本操作,属于面试中比较简单的题目。做法是维护两个指针,一个保留当前有...
  • linhuanmars
  • linhuanmars
  • 2014-02-27 05:12
  • 10904

LeetCode 80. Remove Duplicates from Sorted Array II 解题报告

80. Remove Duplicates from Sorted Array II My Submissions Question Total Accepted: 66554 Total Submissions: 207698 Diff...
  • bruce128
  • bruce128
  • 2016-02-19 13:42
  • 867

Remove Duplicates from Sorted Array [python解]

题目要求:Givena sorted array, remove the duplicates in place such that each element appearonly once and return the new length. Do not allocate e...
  • nadizhong4136
  • nadizhong4136
  • 2017-07-24 19:18
  • 265

LeetCode:Remove Duplicates from Sorted Array

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate ex...
  • fytain
  • fytain
  • 2013-09-02 20:42
  • 2757

LeetCode 26 — Remove Duplicates from Sorted Array(C++ Java Python)

题目:http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove the duplicates in place such that each element ...
  • dragon_dream
  • dragon_dream
  • 2014-02-23 15:12
  • 3851

LeetCode(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 A = [1,1,1,2,2,3], ...
  • feliciafay
  • feliciafay
  • 2015-01-11 03:55
  • 1295

26. Remove Duplicates from Sorted Array (python)

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space f...
  • qq_28119401
  • qq_28119401
  • 2016-10-30 15:58
  • 646

leetcode:82. Remove Duplicates from Sorted List II(Java)解答

转载请注明出处:z_zhaojun的博客 原文地址:http://blog.csdn.net/u012975705 题目地址:https://leetcode.com/problems/reverse-linked-list-ii/ Remove Duplicates from Sorted...
  • u012975705
  • u012975705
  • 2015-12-27 00:18
  • 1366
    个人资料
    • 访问:18682次
    • 积分:726
    • 等级:
    • 排名:千里之外
    • 原创:52篇
    • 转载:4篇
    • 译文:2篇
    • 评论:1条
    最新评论