题目内容

本题来源LeetCode

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.

题目思路

本题难度:medium

这个题目的思路就是经典的快慢指针方法。假如nums的长度小于等于2,则直接返回nums数组。然后设立两个指针i,index。初始化都指向第3个元素,快指针为i,慢指针为index。假如nums[i]!=nums[index-2],则慢指针加一。

Python代码

class Solution(object):
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
l=len(nums)
if l<=2:
return l
index=2
for i in range(2,l):
if nums[i]!=nums[index-2]:
nums[index]=nums[i]
index+=1
return index

[算法题] Remove Duplicates from Sorted Array ii的更多相关文章

  1. [算法题] Remove Duplicates from Sorted Array

    题目内容 本题来源于LeetCode Given a sorted array, remove the duplicates in place such that each element appea ...

  2. 【算法】LeetCode算法题-Remove Duplicates from Sorted Array

    这是悦乐书的第149次更新,第151篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第8题(顺位题号是26).给定一个已经排序(由小到大)的整数数组(元素可以重复),计算其 ...

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

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

  4. 【leetcode】Remove Duplicates from Sorted Array II

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. Discuz论坛提速优化技巧

    Discuz是国内最受站长们欢迎的建站源码之一,除了开源以外还有着很强大的后台,即便是没有建站基础和不懂代码的站长也能很快的架设出一个论坛,甚至是门户. 一个网站的加载速度除了影响你在搜索引擎里的排名 ...

  2. 每篇半小时1天入门MongoDB——4.MongoDB索引介绍及数据库命令操作

    准备工作 继续连接到mongo C:\Users\zouqi>mongo MongoDB shell version: 3.0.7 connecting to: test 查看数据库和集合 &g ...

  3. 最长非降子序列的O(n^2)解法

    这次我们来讲解一个叫做"最长非下降子序列"的问题及他的O(n^2)解法. 首先我们来描述一下什么是"最长非下降子序列". 给你一个长度为n的数组a,在数组a中顺 ...

  4. jquery.jqprint-0.3.js打印table表格遇到的坑

    在谷歌控制台输入window.print();可以调起当前整个页面的打印预览,那么要想打印页面某块区域内容怎么办呢? 我找到了jqprint插件,其原理是运用iframe 元素创建另外一个文档的内联框 ...

  5. 算法——蛮力法之选择排序和冒泡排序c++实现

    这次实现的是蛮力法中的两个例子,选择排序法和冒泡排序法,使用的编译环境是vs2013,下面对这两个算法做一个简单介绍,然后是两个算法的c++实现代码. 选择排序法比较的范围是整个列表,每次扫描结束找出 ...

  6. usaco training 4.1.1 麦香牛块 题解

    Beef McNuggets题解 Hubert Chen Farmer Brown's cows are up in arms, having heard that McDonalds is cons ...

  7. Windows平台下搭建MySQL数据库

    1.下载安装MySQL数据库: (1)->我的标签->软件下载->计算机相关专业所用软件---百度云链接下载->mysql-installer-community-5.7.18 ...

  8. SVN常见问题

    one or more files are in a conflicted state.(一个或多个文件处于矛盾状态)意思是这个文件已经被其他人修改过了. 然后我点击ok按钮后,找到冲突的文件再次up ...

  9. ajax知识点总结

    一.JSON JSON是JavaScript  Object  Notation 的首字母缩写,单词的意思是javascript对象表示法,这里说的json指的是类似于javascript对象的一种数 ...

  10. java加减的二进制实现

    Java中整数基本类型有byte,short,int,long,大小分别为1.2.4.8个字节,一个字节大小为8位,也就是8个二进制码(0/1)组成. 计算机中二进制码分为原码,反码,补码.在计算机中 ...