【leetcode】【40%】【M】Remove Duplicates from Sorted Array II

原创 2015年11月19日 16:48:25


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

跟前一道题倒是没什么太大差别,修改一下判断条件。

最后面的一个元素的处理容易出问题


class Solution(object):
    def removeDuplicates(self, nums):
       
        l = len(nums)
        if l <= 2:
            return l
        index = 0
        i = 0
        while i<l:
            if i+1<l and nums[i] != nums[i + 1]:
                nums[index] = nums[i]
                index += 1
                i += 1
                continue
            elif i+1 == l:
                nums[index] = nums[i]
                index += 1
                return index
            else:
                temp = nums[i]
                if index >= l-1:
                    nums[index] = temp
                    return l
                else:
                    nums[index],nums[index+1] = temp,temp
                    i += 2
                    index += 2
                if index > l-1:
                    return l
                while i < l and nums[i] == temp:
                    i += 1
                   
            #print index,i,nums
        #print nums
        return index
               
           
       
        """
        :type nums: List[int]
        :rtype: int
        """
       


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

LeetCode | Remove Duplicates from Sorted Array II

题目: Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example,...

LeetCode 之 Remove Duplicates from Sorted Array I II — C 实现

Remove Duplicates from Sorted Array I  Given a sorted array, remove the duplicates in place suc...

leetcode 80. Remove Duplicates from Sorted Array II

leetcode 80. Remove Duplicates from Sorted Array II 题目大意:从排序好的数组中删除重复元素,最多允许重复次数为2次 解题思路:对于排序的数组,如果重...

Leetcode_80_Remove Duplicates from Sorted Array II

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43835055 Follow up for "...

LeetCode – Remove Duplicates from Sorted Array II (Java)

Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, G...

leetcode 虐我篇之(六)Remove Duplicates from Sorted Array II

在做完Remove Duplicates from Sorted Array后就想着做这道题了,以为像链表那题一样是完全去掉重复出现的数,

LeetCode 80. Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Giv...

LeetCode080 Remove Duplicates from Sorted Array II

详细见:leetcode.com/problems/remove-duplicates-from-sorted-array-ii Java Solution: github packa...

LeetCode | Remove Duplicates from Sorted Array II(删除重复的元素2)

Remove Duplicates from Sorted Array II

LeetCode---Remove Duplicates from Sorted Array II

问题是:去掉已排序数组中重复两次超过两次的重复元素,即多于两次的时,保留两次重复元素。然后返回新的数组的大小。并不需要考虑新数组大小后边的元素是什么。
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)