[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 ...
随机推荐
- php导出CSV时,超长数字精度丢失问题与前导0的字符串丢失0的问题解决
php生成的CSV有时候会遇到两个特殊情况: 1.输出的字段中,含有超长数字(18位的数字)比方身份证:122121197410180016,就算输出时字段加上"",还是会被识别成 ...
- makefile之cmake入门
cmake是一款生成makefile的软件:在生成makefile之前,首先是写一个CMakeLists.txt文件: 以下为典型例子: 先看目录tree, 在test文件夹中有:include目录, ...
- Matlab中用内建函数代替for循环
在使用matlab进行矩阵计算的时候,经常会遇到要使用for循环的情况.但其实很多操作可以用内部的一些函数代替. bsxfun, arrayfun, cellfun, spfun, structfun ...
- python 格式化日期
#!/usr/bin/env pythonimport sysimport reimport datetime dd = '2014-08-10'da = datetime.datetime.strp ...
- Nio Server
package org.fxc.nio.server; import java.io.FileInputStream; import java.io.IOException; import java. ...
- Spark源码学习2
转自:http://www.cnblogs.com/hseagle/p/3673123.html 在源码阅读时,需要重点把握以下两大主线. 静态view 即 RDD, transformation a ...
- jQuery实时获取checkbox状态问题
在最近的项目开发中,使用jQuery操作checkbox时,发现一个问题. Html代码如下: <body> <div> <inputtype="checkbo ...
- ThinkPHP中的CURD操作
<?php //查询多条记录,返回二维数组 $result = M("admin")->select(); $result = M("admin") ...
- JAVA并发,CountDownLatch使用
该文章转自:http://www.itzhai.com/the-introduction-and-use-of-a-countdownlatch.html CountDownLatch 1.类介绍 一 ...
- Codeforces 713C Sonya and Problem Wihtout a Legend(单调DP)
[题目链接] http://codeforces.com/problemset/problem/713/C [题目大意] 给出一个数列,请你经过调整使得其成为严格单调递增的数列,调整就是给某些位置加上 ...