LeetCode专题-Python实现之第26题:Remove Duplicates from Sorted Array
相关代码已经上传到github:https://github.com/exploitht/leetcode-python
文中代码为了不动官网提供的初始几行代码内容,有一些不规范的地方,比如函数名大小写问题等等;更合理的代码实现参考我的github repo
1、读题
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example,
Given input array nums = [1,1,2],
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.
一个有序数字,要求去掉重复的元素然后返回新的长度,不要申请额外空间。
举个例子,给定数组:[1,1,2],程序需要处理成[1,2,…]然后返回2,也就是前2个元素是去重之后得到的结果,超过2的部分是什么无所谓,是1也行是2也行。
2、解题
这类题想明白了其实解题很简单,重复的需要去掉,无非就是遍历数组,发现重复,就把后面的往前移,把重复值覆盖掉。具体说,可以维护2个指针,慢指针开始指向数组第一个元素,快指针指向第二个元素,然后快指针不断判断自己当前元素和前一个元素是否相同,相同则快指针后移,不相同则将当前值赋值给慢指针的后一个元素,慢指针后移。最后慢指针指向的元素及前面所有元素都是不重复的。具体过程参考如下代码和注释:
class Solution(object):
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
# 删除列表中的重复项,返回操作后的长度
# [1,1,1,2,3,4,4,4,5] -> [1,2,3,4,5] 5
# 维护2个索引,慢的s,快的f;s指向第一个元素,f的指向第二个元素;
# 判断f和f前一个元素是否相等,相等则f后移;不等则s后移一个,值给s,然后f也后移
if len(nums) <= 1:
return len(nums)
s = 0
for f in range(1, len(nums)):
if nums[s] != nums[f]:
s += 1
nums[s] = nums[f]
return s + 1
LeetCode专题-Python实现之第26题:Remove Duplicates from Sorted Array的更多相关文章
- leetcode第26题--Remove Duplicates from Sorted Array
problem: Given a sorted array, remove the duplicates in place such that each element appear only onc ...
- [LC]26题 Remove Duplicates from Sorted Array (删除排序数组中的重复项)(双指针法)(原地实现)
①中文题目 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成 ...
- 【26】Remove Duplicates from Sorted Array
[26]Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...
- Leetcode 题目整理-6 Swap Nodes in Pairs & Remove Duplicates from Sorted Array
24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
- LeetCode专题-Python实现之第21题:Merge Two Sorted Lists
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- 【LeetCode算法-26】Remove Duplicates from Sorted Array
LeetCode第26题 Given a sorted array nums, remove the duplicates in-place such that each element appear ...
- LeetCode(26)题解:Remove Duplicates from Sorted Array
https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove the ...
- [算法题] Remove Duplicates from Sorted Array ii
题目内容 本题来源LeetCode Follow up for "Remove Duplicates": What if duplicates are allowed at mos ...
- [算法题] Remove Duplicates from Sorted Array
题目内容 本题来源于LeetCode Given a sorted array, remove the duplicates in place such that each element appea ...
随机推荐
- 机器学习——KMeans
导入类库 from sklearn.cluster import KMeans from sklearn.datasets import make_blobs import numpy as np i ...
- Class 和 普通构造函数区别
1. Class 在语法上更加贴合面向对象的写法 2. Class在实现继承上更加易读.易理解 3. 更易于写java等后端语言 4.本质还是语法糖,使用prototype
- 弄懂CNN,然后提升准确率4.21-4.27
英语: 1.每天背单词,75起步.(这周没怎么背,考虑调整了) 2.并背王江涛图画作文一:传统文化(这周没背,但肯定要做) 学校: 0.吴恩达ML 1.毕设一:可视化,肺癌基因突变,深度学习(那么作图 ...
- Servlet 监听器Listner
定义: 专门用于对其他对象身上发生的事件或状态改变进行监听和相应处理的对象,当被监视的对象发生情况时,立即采取相应的行动. Servlet 规范为每种事件监听器都定义了相应的接口,它用于监听 ...
- C语言复习5_调试
使用CodeBlocks调试程序 首先要注意,只有打开projects(.cbp文件)的情况下才能debug,单独打开.c文件是不能debug的 1.在行号旁边左键,出现红点,表示为断点breakpo ...
- MyEclipse最新版-版本更新说明及下载 - MyEclipse官方中文网
http://www.myeclipsecn.com/learningcenter/myeclipse-update/ [重要更新]MyEclipse 2015正式版发布 [重要更新]MyEclips ...
- 又到毕业季,尚学堂喊你免费领取100个Java毕设项目(含源码视频),限时一周哦!
你还在为毕设发愁?不知道该如何命题?不知道从哪里下手?担心毕设过不了影响毕业? 尚学堂首家隆重推出了刷爆朋友圈的毕设100个项目,别说你还没去下载观看!!最最重要的是:免费!免费!免费!而且限时一周! ...
- [Swift]LeetCode48. 旋转图像 | Rotate Image
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- [Swift]LeetCode121. 买卖股票的最佳时机 I | Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- [Swift]LeetCode188. 买卖股票的最佳时机 IV | Best Time to Buy and Sell Stock IV
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...