leetcode: 80. Remove Duplicates from Sorted Array II
来源:互联网 发布:当天 today sql 编辑:程序博客网 时间:2024/05/20 11:35
Q
Follow up for “Remove Duplicates”:
What if duplicates are allowed at most twice?
For example,
Given sorted array nums = [1,1,1,2,2,3],
Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn’t matter what you leave beyond the new length.
AC
class Solution(object): def removeDuplicates(self, nums): """ :type nums: List[int] :rtype: int """ i = 0 for n in nums: if i < 2 or n > nums[i-2]: nums[i] = n i += 1 return i# Time: O(n)# Space: O(1)class Solution2(object): def removeDuplicates(self, A): if not A: return 0 last, i, same = 0, 1, False while i < len(A): if A[last] != A[i] or not same: same = A[last] == A[i] last += 1 A[last] = A[i] i += 1 return last + 1if __name__ == "__main__": assert Solution().removeDuplicates([1, 1, 1, 2, 2, 3]) == 5
阅读全文
0 0
- leetcode——26. Remove Duplicates from Sorted Array && 80. Remove Duplicates from Sorted Array II
- LeetCode 26. Remove Duplicates from Sorted Array && 80. Remove Duplicates from Sorted Array II
- leetcode.array--80. 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
- Leetcode: Remove Duplicates from Sorted Array II
- 【leetcode】Remove Duplicates from Sorted Array II
- 批量导入数据从一张表复制到另一张表
- 使用机构证书为jar包签名
- Hive入门操作使用及常用配置
- FAST特征点检测
- 物料帐结算,库存差异为何在下月初冲回
- leetcode: 80. Remove Duplicates from Sorted Array II
- sql语句纵向转横向
- Python之Pandas库
- Git pull、push 免账号密码输入
- CDH5.4.7安装Hadoop集群Java检查不一致
- MongoDB 3.0+访问数据库的方法
- 工业界|谷歌前工程师注册AI教会:创造上帝机器人统治人类
- Some basic tips about 'git stash'
- 干货|采用Istio实现灰度发布(金丝雀发布)