[leetcode] python Remove Duplicates from Sorted Array II
来源:互联网 发布:关于网络信息收集 编辑:程序博客网 时间:2024/06/11 01:42
problem:
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]
.
solution:
法一
class Solution: # @param A a list of integers # @return an integer def removeDuplicates(self,A): n = len(A) if n ==0 : return 0 index = 0 times = 0 for i in xrange(n): if A[index] == A[i]: times += 1 if times ==2 : index += 1 A[index] = A[i] else: index += 1 A[index] = A[i] times = 1 A = A[:index+1] return len(A)法二
class Solution: # @param A a list of integers # @return an integer def removeDuplicates(self,A): n = len(A) if n <= 2 : return n index = 2 for i in xrange(2,n): if A[i] != A[index-2]: A[index] = A[i] index += 1 A = A[:index] return len(A)
0 0
- 【Leetcode】【python】Remove Duplicates from Sorted Array/Remove Duplicates from Sorted Array II
- [leetcode] python Remove Duplicates from Sorted Array II
- 【LeetCode with Python】 Remove Duplicates from Sorted Array II
- 【LEETCODE】80-Remove Duplicates from Sorted Array II [Python]
- LeetCode: Remove Duplicates from Sorted Array II
- [Leetcode] Remove Duplicates from Sorted Array II
- LeetCode: Remove Duplicates from Sorted Array II
- [Leetcode] Remove Duplicates from Sorted Array II
- [LeetCode] Remove Duplicates from Sorted Array II
- [leetcode] Remove Duplicates from Sorted Array II
- [LeetCode]Remove Duplicates from Sorted Array II
- [leetcode]Remove Duplicates from Sorted Array II
- LeetCode-Remove Duplicates from Sorted Array II
- [leetcode] Remove Duplicates from Sorted Array II
- LeetCode - Remove Duplicates from Sorted Array II
- LeetCode:Remove Duplicates from Sorted Array II
- LeetCode | Remove Duplicates from Sorted Array II
- Leetcode: Remove Duplicates from Sorted Array II
- 【黑马程序员】01-OC的特点
- 多媒体类 java
- LeetCode: Count and Say [037]
- ProtoBuffer
- 《C++ Primer第四版》学习笔记(四)变量和基本类型 - 字面值常量
- [leetcode] python Remove Duplicates from Sorted Array II
- 表空间传输
- JAVA顺序读取Properties文件
- OCP-1Z0-051 第150题 集合运算MINUS,INTERSECT
- 装逼第一弹——Fourier变换的意义(答强哥问)
- 黑马程序员---空中网面试题
- TCP协议3次握手/4次握手
- 绽放青春,播撒希望
- VC++常用代码段