导航页-LeetCode专题-Python实现

相关代码已经上传到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的更多相关文章

  1. leetcode第26题--Remove Duplicates from Sorted Array

    problem: Given a sorted array, remove the duplicates in place such that each element appear only onc ...

  2. [LC]26题 Remove Duplicates from Sorted Array (删除排序数组中的重复项)(双指针法)(原地实现)

    ①中文题目 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成 ...

  3. 【26】Remove Duplicates from Sorted Array

    [26]Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...

  4. 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 ...

  5. LeetCode专题-Python实现之第21题:Merge Two Sorted Lists

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  6. 【LeetCode算法-26】Remove Duplicates from Sorted Array

    LeetCode第26题 Given a sorted array nums, remove the duplicates in-place such that each element appear ...

  7. LeetCode(26)题解:Remove Duplicates from Sorted Array

    https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove the ...

  8. [算法题] Remove Duplicates from Sorted Array ii

    题目内容 本题来源LeetCode Follow up for "Remove Duplicates": What if duplicates are allowed at mos ...

  9. [算法题] Remove Duplicates from Sorted Array

    题目内容 本题来源于LeetCode Given a sorted array, remove the duplicates in place such that each element appea ...

随机推荐

  1. Android中Adapter类的使用 “Adapter”

    Adapter用来把数据绑定到扩展了AdapterView类的视图组(例如:ListView或Gallery).Adapter负责创建代表所绑定父视图中的底层数据的子视图. 可以创建自己的Adapte ...

  2. jquery固定表头和列头

    1.对网上的开源方法稍作了些修改 <script type="text/javascript">// <![CDATA[ function FixTable(Ta ...

  3. 封装redis

    封装redis import redis # r = redis.Redis() class MyRedis(): def __init__(self,ip,password,port=6379,db ...

  4. lnmp环境一些基本命令行

    使用service启动/停止/重启相关服务 启动/停止/重启 php服务 service php-fpm start/stop/restart 启动/停止/重启 mysql service mysql ...

  5. lvm快照备份数据库(Mysql5.7)

    备份的目的 能够防止由于机械故障以及人为误操作带来的数据丢失,例如将数据库文件保存在了其它地方. 备份的分类 以操作过程中服务的可用性分: 冷备份:cold backup mysql服务关闭,mysq ...

  6. vue font-icon 图标

    1.vue 游览器左上角小图标 把.ico文件放在根目录下的static文件夹下,然后link标签引入 <link rel="shortcut icon" href=&quo ...

  7. linux 使用sh@d0ws0cks client

    Linux Centos7下安装使用Shadowsocks客户端,实现*** 准备 SS: 搭建一个可以连接外网的服务器 教程可见 自己动手搭梯子 服务器:本人用的腾讯云服务器,系统为Centos7 ...

  8. Ubuntu 16.04下sublime text3安装

    安装方法 在确保Ubuntu更新了国内镜像源的前提下,使用ppa安装: sudo add-apt-repository ppa:webupd8team/sublime-text-3 sudo apt- ...

  9. HTTPS 原理浅析及其在 Android 中的使用

    作者:曹丰斌   本文首先分析HTTP协议在安全性上的不足,进而阐述HTTPS实现安全通信的关键技术点和原理.然后通过抓包分析HTTPS协议的握手以及通信过程.最后总结一下自己在开发过程中遇到的HTT ...

  10. ansible基础-加密

    一 简介 注:本文demo使用ansible2.7稳定版 众所周知,ansible是很火的一个自动化部署工具,在ansible控制节点内,存放着当前环境服务的所有服务的配置信息,其中自然也包括一些敏感 ...