题目简述

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.

解题思路

class Solution(object):
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
d = {}
l = len(nums)
if l < 2:
return l
i = 0
while True:
if i >= l:
break
if nums[i] not in d:
d[nums[i]] = 1
else:
d[nums[i]] += 1
if d[nums[i]] > 2:
nums.remove(nums[i])
l -= 1
continue
i += 1 return l

Remove Duplicates from Sorted Array II的更多相关文章

  1. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  2. 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element

    Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...

  3. Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II

    以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置 一:Remove Element Given an array and a value, remove a ...

  4. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

  5. 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  6. [leetcode] 80. Remove Duplicates from Sorted Array II (Medium)

    排序数组去重题,保留重复两个次数以内的元素,不申请新的空间. 解法一: 因为已经排好序,所以出现重复的话只能是连续着,所以利用个变量存储出现次数,借此判断. Runtime: 20 ms, faste ...

  7. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

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

  8. [Leetcode] Remove Duplicates From Sorted Array II (C++)

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

  9. LeetCode OJ Remove Duplicates from Sorted Array II

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

  10. 算法题丨Remove Duplicates from Sorted Array II

    描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? 示例 Giv ...

随机推荐

  1. MySQL性能调优my.cnf详解

    [client] port = 3306 socket = /tmp/mysql.sock [mysqld] port = 3306 socket = /tmp/mysql.sock basedir  ...

  2. 使用django开发博客过程记录1——数据库设计

    1.数据库设计 2.插入测试数据 3.配置相关问题 1.数据库设计 数据库有简单的三张表:Article.Category.Tag以下是代码 # -*- coding:utf-8 -*- from _ ...

  3. php程序 注册机制

    密码加密方式 1.直接md5加密 2.md5和随机数(也可以是固定参数,如(Rfd4WE784)) 3.md5(md5) 4. 参考 function sp_password($pw,$authcod ...

  4. metaWeblog Test

     #include "stdio.h" int main(int argc ,char **argv) { printf("argc = %d \n",arg ...

  5. Error configuring application listener of class。。。NoClassDefFoundError。。某Listener 之启动tomcat报错

    当你启动tomcat的时候如果报类似下面的错误: WARNING: [SetContextPropertiesRule]{Context} Setting property 'source' to ' ...

  6. 商业智能BI

    参考资料: 7 款顶级开源 BI(商务智能)软件和报表工具

  7. 如何保存gnome的linux的 会话?相当于windows下的休眠?

    在关机前, 你进行的所有操作, 的集合, 就叫做你跟 linux系统 机器间的 一次 会话, 一个session. linux 可以 在关机时保存 这些session, 保存这些打开的窗口 和程序. ...

  8. AngularJS2.0 教程系列(一)

    http://my.oschina.net/u/2275217/blog/482178

  9. mac 关闭&&显示隐藏文件命令

    打开终端,输入: defaults write com.apple.finder AppleShowAllFiles -bool true 此命令显示隐藏文件 defaults write com.a ...

  10. 学习 opencv---(6)玩转opencv源代码:生成opencv 工程解决方案与opencv 源码编译

    在这篇中,我们探讨如何通过已安装的opencv选择不同的编译器类型,生成高度还原的OpenCV开发时的解决方案工程文件,欣赏OpenCV新版本中总计 六十六多万行的精妙源代码.我们可以对其源代码进行再 ...