题目来源:

  https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/


题意分析:

  跟定一个排好序的数组。修改这个数组使得每个数最多可以出现两次。返回去掉多余重复数组后的长度,当然将多余重复数放到数组后面并不影响。


题目思路:

  利用两个下标,一个用来遍历数组,另外一个来记录新数组,用一个bool变量来记录是否这个数值已经访问过1次。时间复杂度O(n)。


代码(Python):

  

class Solution(object):
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
size = len(nums)
if size == 0 or size == 1:
return size
begin,i = 1,1
tmp,visit = nums[0],False
while i < size:
if nums[i] == tmp:
if not visit:
nums[begin] = tmp
begin += 1;visit = True
else:
tmp,visit = nums[i],False
nums[begin] = tmp
begin += 1
i += 1
return begin

转载请注明出处:http://www.cnblogs.com/chruny/p/5088571.html

[LeetCode]题解(python):080-Remove Duplicates from Sorted Array II的更多相关文章

  1. 【LeetCode】080. Remove Duplicates from Sorted Array II

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

  2. Java for LeetCode 080 Remove Duplicates from Sorted Array II

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

  3. LeetCode(80)Remove Duplicates from Sorted Array II

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

  4. 080 Remove Duplicates from Sorted Array II 从排序阵列中删除重复 II

    “删除重复项目” 的进阶:如果重复最多被允许两次,又该怎么办呢?例如:给定排序数列 nums = [1,1,1,2,2,3]你的函数应该返回长度为 5,nums 的前五个元素是 1, 1, 2, 2 ...

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

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

  6. 【leetcode】Remove Duplicates from Sorted Array II

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

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

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

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

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

  9. 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 ...

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

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

随机推荐

  1. zyUpload界面绝佳、体验超棒的HTML5上传插件

    一.为毛线开发它 经过了两个星期做出了两个基于HTML5的多文件上传插件,之前在做网站的时候用到文件上传这一个功能,但是大多说都是基于Flash的,正好最近HTML5很火,而且渐渐壮大起来,感觉搞前端 ...

  2. Android Popupwindow 拖动

    版本号:1.0 日期:2014.4.29 版权:© 2014 kince 转载注明出处 关于View的拖动大家应该比較了解了,比方对一个控件IamgeView拖动,或者一个视图View拖动,实现方式也 ...

  3. Swift 委托/代理设计模式

    Swift 中的委托/代理模式(以下简称"代理模式")与object-c的代理模式基本一致. 代理模式的基本思想就是将我(类或者结构体等)需要来完成的工作交给(委托给)另一个有我所 ...

  4. JavaScript之数组学习

    在JavaScript中,数组用关键字Array来声明.声明数组的同时还可以指定数组初始元素的大小,也就是数组的长度;下面代码定义了一个数组长度为6的数组; var beatles=Array(6); ...

  5. ##DAY10 UITableView基础

    ##DAY10 UITableView基础 UITableView继承于UIScrollView,可以滚动. UITableView的每⼀条数据对应的单元格叫做Cell,是UITableViewCel ...

  6. Hadoop 处理“Name node is in safe mode”问题(转)

    运行hadoop程序时,有时候会报以下错误:org.apache.hadoop.dfs.SafeModeException: Cannot delete /user/hadoop/input. Nam ...

  7. mybatis之动态SQL

    <if>的使用 如果第一个if不成立的话可能会出现where and的语法错误,解决方法是在外层加<where>标签,此时如果以and和or衔接where的话会被删除. < ...

  8. 帝国cms中上一篇与下一篇个性化灵动标签调出

    这里的上下篇是用灵动标签制作,可以更为个性化 下一篇 <a href="<?phpecho $bqsr[titleurl];$next='true';?>"> ...

  9. c++ 静态多态与动态多态

    多态polymorphism是指具有多种形态的情况,它能根据单一的标记关联不同的行为.多态是面向对象程序设计的基础.在面向对象程序设计中的多态是一种运行时的多态.C++中有两种多态,称为动多态(运行时 ...

  10. 01-复杂度2. Maximum Subsequence Sum (25)

    Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...