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 ...
随机推荐
- windows 下设置MTU数值
输入:netsh interface ipv4 show subinterfaces 查询到目前系统的MTU值.再分别输入一行按一次回车键. netsh interface ipv4 set subi ...
- [转]Tomcat日志详解
Tomcat下相关的日志文件: 1.Cataline引擎的日志文件,文件名为catalina.{date}.log 2.Tomcat下内部代码丢出的日志,文件名为localhost.{date}.lo ...
- intellij idea中设置SVN插件教程
1.选择VCS→Browser VCS Repository→Browse Subversion Repository 2.在弹出的SVN Repository菜单中,选择左上角的绿色“+”号,填写S ...
- ssh登录卡住问题
使用ssh登录远程centos服务器,卡住不动 系统centos 7 加-v查看登录过程,一直卡在这里 解决:我的是mtu问题 将eth0 mtu 默认1500修改为1200就可以了 ifconfig ...
- 在MVC中加载view(点开链接)的方式
主要有: Html.ActionLink Html.RenderPartial Html.RenderAction Html.Partial AJAX.ActionLink load 浏览器对象模型 ...
- IOS DatePicker 和 UIBarButtonItem 常用属性
- (void)viewDidLoad { [super viewDidLoad]; // // self.inputTextField.inputView = [[UISwitch alloc ] ...
- 集成Ehcache用来缓存表以后,怎么设置缓存刷新时间
问答 集成Ehcache用来缓存表以后,怎么设置缓存刷新时间 发布于 217天前 作者 老司机 93 次浏览 复制 上一个帖子 下一个帖子 标签: 无 集成Ehcache用来缓存表以后, ...
- Mybatis-延迟加载和缓存
延迟加载 概念: 就是在需要用到数据时才进行加载,不需要用到数据时就不加载数据.延迟加载也称懒加载. 好处:先从单表查询,需要时再从关联表去关联查询,大大提高数据库性能,因为查询单表要比关联查询多张表 ...
- opencv与灰度图
https://blog.csdn.net/qq_32211827/article/details/56854985 首先,灰度图可以是一个通道存成图片,也可以是3个通道存成图片,3个通道存成图片,其 ...
- vs code vim
很多初学者启动vim后,不知道怎么输入字符:按了半天字母,结果屏幕还是空的. vim和记事本或WORD不一样,不是一打开后就可以输入文字,此时它处于正常模式. vim一共有4个模式: 正常模式 (No ...