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 ...
随机推荐
- Java字节流与字符流的区别详解
字节流与字符流 先来看一下流的概念: 在程序中所有的数据都是以流的方式进行传输或保存的,程序需要数据的时候要使用输入流读取数据,而当程序需要将一些数据保存起来的时候,就要使用输出流完成. 程序中的输入 ...
- 不得不承认pretty-midi很好用,以及一些简单的上手
官方文档在此: http://craffel.github.io/pretty-midi/ 首先我们演示如何将midi文件转变为piano-roll格式(matrix). 现在我们手中有了一个数据集, ...
- 9 Palindrome_Number
Determine whether an integer is a palindrome. Do this without extra space. 判断一个数是否是回文数. public class ...
- linux 命令——39 grep (转)
Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达 ...
- js清空表单数据的方式(遍历+reset)
方法1:遍历页面元素 /* 清空FORM表单内容 id:表单ID*/ function ClearForm(id) { var objId = document.getElementById(id); ...
- Java时间为什么从1970-01-01 00:00:00 000开始
不仅仅是Java,几乎所有的语言的时间都是从这一刻开始算起的. 原因:java起源于UNIX系统,而UNIX认为1970年1月1日0点是时间纪元. 最初计算机操作系统是32位,而时间也是用32位表示. ...
- mongo 4.0以下版本 类型转换
.文档格式 "Values" : [ { "key" : "姓名", "value" : "jenny&quo ...
- Java设计模式学习——简单工厂
一. 定义与类型 定义:有工程对象决定创建出哪一种产品类的实例 类型:创建型,但不属于GOF23中设计模式 二. 适用场景 工厂类负责创建的对象比较少 客户端(应用层)只知道传入工厂类的参数,对于如何 ...
- java常用 开源
http://sourceforge.nethttp://code.google.com/hosting/http://www.open-open.com/code/tags/Javahttp://w ...
- CSVDE
csvde -f C:\export_OrganizationalUnit.csv -r '(objectClass=organizationalUnit)' -l 'displayName,prox ...