题目:

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

For example,
Given sorted array A = [1,1,1,2,2,3],

Your function should return length = 5, and A is now [1,1,2,2,3].

代码:oj测试通过 Runtime: 120 ms

 class Solution:
# @param A a list of integers
# @return an integer
def removeDuplicates(self, A):
if len(A) < 3 :
return len(A) pre = 0
curr = 1
for i in range(2,len(A)):
if A[i] == A[curr] and A[i] == A[pre]:
continue
else:
A[curr+1],A[i] = A[i],A[curr+1]
pre += 1
curr += 1 return curr+1

思路

首先长度小于3的可以直接返回,这省去不少事。

主要的逻辑是:从第三个元素开始判断,当前元素i是否与前两个元素相等;如果相等,则i继续往下走;如果不等,则交换curr+1与i位置的元素

刷了几道了Array的题,感觉有一些相似的技巧,就是只遍历一次,不满足条件就交换元素位置。不占用额外的空间。

leetcode 【 Remove Duplicates from Sorted Array II 】python 实现的更多相关文章

  1. [leetcode]Remove Duplicates from Sorted Array II @ Python

    原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 题意: Follow up for &quo ...

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

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

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

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

  4. [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素

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

  5. [LeetCode] Remove Duplicates from Sorted Array II [27]

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

  6. [leetcode]Remove Duplicates from Sorted List II @ Python

    原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ 题意: Given a sorted link ...

  7. LeetCode Remove Duplicates from Sorted Array II 删除整型数组中的重复元素并返回剩下元素个数2

    class Solution { public: int removeDuplicates(int A[], int n) { ],*e=&A[]; //s指向“连续数字”的第一个,e往后遍历 ...

  8. LeetCode:Remove Duplicates from Sorted Array I II

    LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...

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

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

  10. 【leetcode】Remove Duplicates from Sorted Array II

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

随机推荐

  1. jQuery_2_常规选择器-简单选择器

    JQuery最核心的组成部分就是:选择器引擎.它继承了css的语法,可以对DOM元素的标签名.属性名.状态等进行快速准确的选择. jQuery选择器的写法与CSS 选择器十分类似,只不过他们的功能不同 ...

  2. oracle数据类型及操作

    1. Oracle字符串操作 1.1 字符串类型 Ø CHAR和VARCHAR2类型 l CHAR存放定长字符,如果数据存不满指定长度则用空格补齐,CHAR类型浪费空间换取查询时间的缩短. l VAR ...

  3. 平时对ES6的一些总结

    1.Genertor中yield和Interator中的next方法 Genertor的yield是把这个函数变成分段的:Interator中的next也是一个一个执行的: function* f() ...

  4. pta 编程题6 树的同构

    其它pta数据结构编程题请参见:pta 题目请参见:树的同构 因题目中左右子树是按照下标给出,因此用数组存放树是更好的方法. 判断两棵树是否同构:用递归的方法.如果当前两个结点都为空,则返回TRUE: ...

  5. 棋盘V(最小费用最大流)

    棋盘V 时间限制: 1 Sec  内存限制: 128 MB提交: 380  解决: 44[提交] [状态] [讨论版] [命题人:admin] 题目描述 有一块棋盘,棋盘的边长为100000,行和列的 ...

  6. 《坐热板凳》第九次团队作业:Beta冲刺与验收准备(第一天)

    <坐热板凳>第九次团队作业:Beta冲刺与验收准备 项目 内容 这个作业属于哪个课程 http://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https ...

  7. .net core 2.2跨域session

    不说废话,直奔主题! 使用场景:微服务.前后端已经跨域.又不得不使用session 使用session的好处:此处省略若干字...... 做法: 客户端:该怎么写就怎么写. 服务端: Startup类 ...

  8. dn.net/blueheart20/article/details/22080489

    dn.net/blueheart20/article/details/22080489

  9. c++异常处理--创建自己的异常处理类

    复习了一下c++中的异常处理! 继承exception类 class myException : public std::exception { public: explicit myExceptio ...

  10. oracle常用运维sql语句

    1.查询dblink语句 col owner for a20col db_link for a30col username for a20col host for a30set linesize 12 ...