leetcode 【 Remove Duplicates from Sorted Array II 】python 实现
题目:
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 实现的更多相关文章
- [leetcode]Remove Duplicates from Sorted Array II @ Python
原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 题意: Follow up for &quo ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [Leetcode] Remove Duplicates From Sorted Array II (C++)
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Duplicates from Sorted Array II [27]
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- [leetcode]Remove Duplicates from Sorted List II @ Python
原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ 题意: Given a sorted link ...
- LeetCode Remove Duplicates from Sorted Array II 删除整型数组中的重复元素并返回剩下元素个数2
class Solution { public: int removeDuplicates(int A[], int n) { ],*e=&A[]; //s指向“连续数字”的第一个,e往后遍历 ...
- LeetCode:Remove Duplicates from Sorted Array I II
LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...
- 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 ...
随机推荐
- swagger + springboot
参考文章: https://blog.csdn.net/xupeng874395012/article/details/68946676/ https://blog.csdn.net/hry2015 ...
- python基础-三元运算和bytes数据
三元运算 进制 二进制,01 八进制,01234567 十进制,0123456789 十六进制,0123456789ABCDEF bytes类型 http://www.cnblogs. ...
- nrm—源管理工具
全局安装 npm install -g nrm 查看可选源 nrm ls 其中,带*的是当前使用的源,上面的输出表明当前源是hiknpm 切换源 nrm use taobao 新增源 nrm add ...
- cmd中不能输入中文
在命令行窗口下不能输入中文,只能输入英文,这是因为cmd.exe窗口启动时并没有启动conime.exe进程.事实上,这个进程容易被后门病毒利用,因此网上很多人都把它当成病毒看待,一些网上下载的系统就 ...
- HDU 1009 FatMouse' Trade肥老鼠的交易(AC代码) 贪心法
题意: 一只老鼠用猫粮来换豆子,每个房间的兑换率不同,所以得尽量从兑换率高的房间先兑换.肥老鼠准备M磅猫粮去跟猫交易,让猫在warehouse中帮他指路,以找到好吃的.warehouse有N个房间,第 ...
- java核心技术 要点笔记2
第4章 对象和类 1.面向对象 类:构造对象的模板,创建类的实例:由类构造对象的过程,封装,继承: 对象:对象的特性——对象的行为,对象的状态,对象的标识: 类之间的关系: 依赖(“user-a” ...
- 汶川大地震中的SAP成都研究院
5·12汶川地震,发生于北京时间(UTC+8)2008年5月12日(星期一)14时28分04秒,此次地震的面波震级 里氏震级达8.0Ms.矩震级达8.3Mw,地震烈度达到11度.地震波及大半个中国及亚 ...
- IOS UITextFieldDelegate (常用的代理方法)
#pragma mark - UITextFieldDelegate // 返回NO代表着文本输入框不可以改变(不可以编辑) - (BOOL)textField:(UITextField *)text ...
- iptables (1) 原理
网上看到这个配置讲解得还比较易懂,就转过来了,大家一起看下,希望对您工作能有所帮助. iptables简介 netfilter/iptables(简称为iptables)组成Linux平台下的包过滤防 ...
- NOIP2018赛前停课集训记(10.24~11.08)
前言 为了不久之后的\(NOIP2018\),我们的停课从今天(\(Oct\ 24th\))起正式开始了. 本来说要下周开始的,没想到竟提早了几天,真是一个惊喜.毕竟明天有语文考试.后天有科学考试,逃 ...