[LeetCode]题解(python):080-Remove Duplicates from Sorted Array II
题目来源:
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的更多相关文章
- 【LeetCode】080. Remove Duplicates from Sorted Array II
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- 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 ...
- LeetCode(80)Remove Duplicates from Sorted Array II
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- 080 Remove Duplicates from Sorted Array II 从排序阵列中删除重复 II
“删除重复项目” 的进阶:如果重复最多被允许两次,又该怎么办呢?例如:给定排序数列 nums = [1,1,1,2,2,3]你的函数应该返回长度为 5,nums 的前五个元素是 1, 1, 2, 2 ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- [leetcode] 80. Remove Duplicates from Sorted Array II (Medium)
排序数组去重题,保留重复两个次数以内的元素,不申请新的空间. 解法一: 因为已经排好序,所以出现重复的话只能是连续着,所以利用个变量存储出现次数,借此判断. Runtime: 20 ms, faste ...
- 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 ...
- Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II
以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置 一:Remove Element Given an array and a value, remove a ...
随机推荐
- 使用MD5完成自定义Person对象的加密过程
---恢复内容开始--- 首先:我们对自定义Person对象的加密过程所用的方法是归档写入文件的方法. 第一步:创建Person,继承于NSObject,然后在Person.h文件遵守NSCoding ...
- lua IDE for cocos2d-x development
原文链接:http://hi.baidu.com/balduc0m/item/648093dad238bd2a39f6f78e lua IDE for cocos2d-x development -- ...
- linux 使用sudo开放普通用户权限
整理一下以前写的东东,刚才又忘了- ---------------------------------------------------------------------------------- ...
- .Net 利用消息在进程间通讯实现进程互操作
有时候我们会遇到需要在两个进程间通过某种方式实现互操作,方法有很多,例如你可以尝试让两个进程持续监视一个外部文件,由此文件记录各自进程的数据:还有可以使用网络端口实现进程间通讯.共享一片内存区域记录及 ...
- Python下载Bing主页图片
直接上代码: # -*- coding: cp936 -*- import urllib import os print 'Download data......' url = 'http://cn. ...
- 从一句SQL得出的启示
select count(*) + 1 from `table` where rank > (select rank from `table` where id = *) 上面那句SQL 给了我 ...
- ORACLE恢复误删除的对象(表、存储过程等)
1.恢复存储过程 原理就是利用了oracle里所有的存储过程的源代码都是存在dba_source里,而drop某个存储过程的时候,oracle这里肯定要去dba_source里把相关的源代码给dele ...
- C# 颜色有3种表示方式: 6位16进制、RGB、 颜色关键字
最常用的是6位16进制的代码表示法.如bgcolor=#ff0000;其中#只是表示使用6位16进制的颜色代码声明颜色.代码的头两位即ff表示三原色中的红色,范围当然是16进制的00-ff,中间两位即 ...
- leetcode Palindrome Number python
class Solution(object): def isPalindrome(self, x): """ :type x: int :rtype: bool &quo ...
- textarea中的空格与换行
当在一个textarea标签中键入一个回车时,实际上会插入2个符号:\n\r在javascript里, line breaks用\n表示when you pull text into Javascri ...