76 leetcode - Remove Duplicates from Sorted Array II
来源:互联网 发布:淘宝店铺违规发布商品 编辑:程序博客网 时间:2024/05/15 12:01
#!/usr/bin/python# -*- coding: utf-8 -*-'''Remove Duplicates from Sorted Array IIFollow 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.'''class Solution(object): def removeDuplicates(self, nums): """ :type nums: List[int] :rtype: int """ length = len(nums) if length <= 2: return length flag = False ret = 1 cur = 1 for index in range(1,length): if nums[index] == nums[index - 1]: if flag == False: ret += 1 flag = True nums[cur] = nums[index] cur += 1 else: flag = False ret += 1 nums[cur] = nums[index] cur += 1 #print nums[:cur] return retif __name__ == "__main__": s = Solution() print s.removeDuplicates([1,1,1,2,2,3]) print s.removeDuplicates([1,1,1,1,1,1,2,2,2,3])
0 0
- 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
- Leetcode: Remove Duplicates from Sorted Array II
- [LeetCode] Remove Duplicates from Sorted Array II
- Leetcode Remove Duplicates from Sorted Array II
- MATLAB R2016a 安装教程
- metasploit根据cve搜索
- 滑动窗口的最大值
- TIP计划总结
- jvm.dll不能加载
- 76 leetcode - Remove Duplicates from Sorted Array II
- linux同步时间
- 【caffe源码研究】第四章:完整案例源码篇(3) :LeNet初始化测试网络
- (迪杰斯特拉)最短路径C语言
- https 改造
- java中private,public,protected详解
- Nginx配置文件(nginx.conf)配置详解(一)
- WIN 创建一个指向某相对路径的快捷方式
- TabLayout.setTabTextColors()的一个Bug