【LeetCode每天一题】Remove Duplicates from Sorted Array II(移除有序数组中重复的两次以上的数字)
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length.Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
Example 1:
Given 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 respectively. It doesn't matter what you leave beyond the returned length.
Example 2:
Given nums = [0,0,1,1,1,1,2,3,3], Your function should return length = 7, with the first seven elements of nums being modified to 0, 0, 1, 1, 2, 3 and 3 respectively. It doesn't matter what values are set beyond the returned length. 思路
这道题主要是需要将排序数组中重复唱过两次以上的数组除去并返回最后的长度, 因此对于此我们可以从头开始遍历,如果后面的元素和当前元素相等,我们使用一个循环来将后面相等的元素直接去除。不相等则遍历下一个元素,最后返回数组的长度。时间复杂度为O(n),空间复杂度为O(1)。
解决代码
class Solution(object):
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums:
return 0
i, nums_len = 0, len(nums) # 开始下标和数组长度
while i < nums_len:
if i < nums_len-1 and nums[i] == nums[i+1]: # i的长度小于nums_len-1的长度,否则nums[i+1]会越界。
i, tem = i+2, nums[i] # 直接将下标i指向 第三个元素
while i < nums_len and tem == nums[i]: # 将后面与tem相同的元素删除
nums.pop(i)
nums_len -= 1 # 因此使用的是pop(),所以每次pop之后数组的长度会减1,但是i的下标可以不表
continue
i += 1
return len(nums) # 返回长度
【LeetCode每天一题】Remove Duplicates from Sorted Array II(移除有序数组中重复的两次以上的数字)的更多相关文章
- LeetCode 26 Remove Duplicates from Sorted Array (移除有序数组中重复数字)
题目链接: https://leetcode.com/problems/remove-duplicates-from-sorted-array/?tab=Description 从有序数组中移除重 ...
- [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项 II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [LeetCode] Remove Duplicates from Sorted List II 移除有序链表中的重复项之二
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项之二
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索之二
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- [算法题] Remove Duplicates from Sorted Array ii
题目内容 本题来源LeetCode Follow up for "Remove Duplicates": What if duplicates are allowed at mos ...
- [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索 II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- leetcode第26题--Remove Duplicates from Sorted Array
problem: Given a sorted array, remove the duplicates in place such that each element appear only onc ...
随机推荐
- api拆分(数据传递和接收的几种方式)
传递方式一:对象转String 接收:String类型接收再转对象 传递方式二:Map 接收:Map 传递方式三:json(Map转json) 接收:String转Map 传递方式四:Map里放jso ...
- MySQL---7、常用操作
1.表列的添加.修改和删除 2.视图 3.字符集和校对集 4.触发器相关知识
- del
Before writing it I spent a fair amount of time on reviewing our chat-log. How silly. asljdfajsodfss ...
- 二、socket编写简单BIO的HTTP服务器
一.目标 诸如tomcat等web服务器中间件简化了我们web的开发成本,但有时候我们或许并不需要这么一个完备的服务器,只是希望做一个简单地处理或者做特殊用途的服务器. 本文将提供一个HTTP的服务器 ...
- java自学-流程控制语句
一件事情从开始到结束,需要先做什么,再做什么,最后再怎么做,这段过程就是一个流程.程序逻辑处理也是一个流程,java中有专门的流程控制语句,主要分为这几种:顺序,判断,选择,循环. 1.顺序流程 顺 ...
- ThreadPoolExecutor线程池中线程不能超过核心线程数量的问题
int arg1=2;//核心线程 int arg2=40;//最大线程数量 int arg3=100;//空余保留时间 ThreadPoolExecutor pool=new ThreadPoolE ...
- eclipse中编写运行c/c++
注意:此过程有点复杂 准备:1.MinGW:c/c++运行环境: 2.CDT 1.MinGW:安装程序:http://sourceforge.net/projects/mingw/?source=ty ...
- 记录一次使用terminal进行git管理与提交到Github的过程
1.环境的构建: 使用Mac系统自带的Git进行版本管理存在,Git是系统的Xcode集成的 查看版本的命令: $ git --version git version (Apple Git-) 查看g ...
- SQL导出到Excel 存储过程
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_exporttb]') and OBJECTPROPER ...
- 您只能在 HTML 输出流中使用 document.write,啥意思
JavaScript :写入到HTML输出语法 注意:只能在HTML输出中使用,如果在文档已经加载后使用(比如在函数中) 会覆盖到整个文档 <!DOCTYPE html> <htm ...