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. java中equal和==的比较

    equals 方法是 java.lang.Object 类的方法. 有两种用法说明: (1)对于字符串变量来说,使用“==”和“equals()”方法比较字符串时,其比较方法不同. “==”比较两个变 ...

  2. spring mongodb查询

    MongoRepository 查询条件 Keyword Sample Logical result After findByBirthdateAfter(Date date) {"birt ...

  3. 十一、curator recipes之联锁InterProcessMultiLock

    简介 curator实现了一个类似容器的锁InterProcessMultiLock,它可以把多个锁包含起来像一个锁一样进行操作,简单来说就是对多个锁进行一组操作.当acquire的时候就获得多个锁资 ...

  4. JavaMail获取已发送邮件

    public static void main(String args[]) { Properties props = new Properties(); // 参数配置 props.setPrope ...

  5. sql:PostgreSQL

    PostgreSQL sql script: -- Database: geovindu -- DROP DATABASE geovindu; CREATE DATABASE geovindu WIT ...

  6. cf1088D. Ehab and another another xor problem(思维)

    题意 题目链接 系统中有两个数\((a, b)\),请使用\(62\)以内次询问来确定出\((a, b)\) 每次可以询问两个数\((c, d)\) 若\(a \oplus c > b \opl ...

  7. NGINX本地服务器解析域名

    1.找到hosts文件 ,添加需要解析的域名 2.在cmd命令窗口中检测解析是否生效 3 找到本地服务器的域名配置文件:添加绑定的域名,更改访问的目录 4.添加pathinfo.隐藏index.php ...

  8. react生命周期es6

    基本函数有 import React from 'react' export default class MyClass extends React.Component { constructor(p ...

  9. js 数据监听--对象的变化

    class Observer { constructor(data) { this.data = data; this.filterObj(data); } static isObject(obj) ...

  10. IIS7 使用server farms 进行负载均衡

    1.首先,到微软官网下载web平台安装程序: https://www.microsoft.com/web/downloads/ 2.安装好后,会在IIS里有这个图标: 3.双击这个图标:安装 4.安装 ...