Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length.Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

Example 1:

 Given 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 respectively.  It doesn't matter what you leave beyond the returned length.

Example 2:

 Given nums = [0,0,1,1,1,1,2,3,3],

 Your function should return length = 7, with the first seven elements of nums being modified to 0, 0, 1, 1, 2, 3 and 3 respectively.

 It doesn't matter what values are set beyond the returned length.

思路

     这道题主要是需要将排序数组中重复唱过两次以上的数组除去并返回最后的长度, 因此对于此我们可以从头开始遍历,如果后面的元素和当前元素相等,我们使用一个循环来将后面相等的元素直接去除。不相等则遍历下一个元素,最后返回数组的长度。时间复杂度为O(n),空间复杂度为O(1)。
解决代码


 class Solution(object):
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums:
return 0
i, nums_len = 0, len(nums) # 开始下标和数组长度
while i < nums_len:
if i < nums_len-1 and nums[i] == nums[i+1]: # i的长度小于nums_len-1的长度,否则nums[i+1]会越界。
i, tem = i+2, nums[i] # 直接将下标i指向 第三个元素
while i < nums_len and tem == nums[i]: # 将后面与tem相同的元素删除
nums.pop(i)
nums_len -= 1 # 因此使用的是pop(),所以每次pop之后数组的长度会减1,但是i的下标可以不表
continue
i += 1
return len(nums) # 返回长度

【LeetCode每天一题】Remove Duplicates from Sorted Array II(移除有序数组中重复的两次以上的数字)的更多相关文章

  1. LeetCode 26 Remove Duplicates from Sorted Array (移除有序数组中重复数字)

    题目链接: https://leetcode.com/problems/remove-duplicates-from-sorted-array/?tab=Description   从有序数组中移除重 ...

  2. [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项 II

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  3. [LeetCode] Remove Duplicates from Sorted List II 移除有序链表中的重复项之二

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  4. [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项之二

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  5. [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  6. [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索之二

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  7. [算法题] Remove Duplicates from Sorted Array ii

    题目内容 本题来源LeetCode Follow up for "Remove Duplicates": What if duplicates are allowed at mos ...

  8. [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索 II

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  9. leetcode第26题--Remove Duplicates from Sorted Array

    problem: Given a sorted array, remove the duplicates in place such that each element appear only onc ...

随机推荐

  1. 网页布局之Div

    div(division分区) 它是一个块标签,主要用来把网页中相关的内容组织到一起 你可以把网页的头部放到一个标签中,主体部分放到另一个标签中 使用class类名描述div内容 要想区分每个div, ...

  2. 记一次线上Mysql数据库 宕机

    从发现问题,到最后解决一共消耗两个半小时(7:30~10:00),报警电话16通,警察坐镇,未完待续 ......

  3. jquery appendTo用法

    $("#top_cartWarp").appendTo($("#top_main_right")).css('position','relative').css ...

  4. Spring中的Bean配置方式

    1.IOC和DI概述 IOC(Inversion of Control):其思想是反转资源获取的方向. 传统的资源查找方式要求组件向容器发起请求查找资源. 作为回应, 容器适时的返回资源. 而应用了 ...

  5. SQL索引优化及实战文章总结(总结)

    1. MySQL索引原理以及查询优化 2.

  6. csharp: Microsoft SqlHelper

    from: Microsoft Data Access Application Block for .NET  https://www.microsoft.com/en-us/download/con ...

  7. CF235C Cyclical Quest

    题意 给定一个长度为\(n\)的母串 \(q\)组询问 这个串可以旋转(就是把最后一位丢到最前面这样子) 问这个串以及其旋转的串在给定的串中出现了多少次 Sol 旋转就把它复制一遍接在后面 然后就在\ ...

  8. 【Angularjs】ng-repeat中使用ng-model遇到的问题

    总结:在ng-repeat中ng-model的问题,原因是ng-model对controller中的$scope是不可见的,所以在使用repeat中的某个对象的属性的时候,最好还是将该对象或者该对象的 ...

  9. Fragment初探

    Fragment允许将Activity拆分成多个完全独立封装的可重用的组件,每个组件有它自己的生命周期和UI布局.Fragment最大的优点是为不同屏幕大小创建灵活的UI.每个Fragment都是独立 ...

  10. 常用到的简单命令 Sublime Git NPM WindowsCMD MacTerminal(Unix命令) Vim

    sublime 选择标签及其内容 ctrl+shift+a连续按两次 调出Package console: Mac: command + shift + p 安装插件: 1.调出 Package co ...