Python实现删除排序数组中重复项的两种方法示例


Posted in Python onJanuary 31, 2019

本文实例讲述了Python实现删除排序数组中重复项的两种方法。分享给大家供大家参考,具体如下:

对于给定的有序数组nums,移除数组中存在的重复数字,确保每个数字只出现一次并返回新数组的长度

注意:不能为新数组申请额外的空间,只允许申请O(1)的额外空间修改输入数组

Example 1:

Given 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

Example 2:

Given nums = [0,0,1,1,1,2,2,3,3,4],Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.
It doesn't matter what values are set beyond the returned length.

说明:为什么返回列表长度而不用返回列表?因为列表传入函数是以引用的方式传递的,函数中对列表进行的修改会被保留。

// nums is passed in by reference. (i.e., without making a copy)
int len = removeDuplicates(nums);
// any modification to nums in your function would be known by the caller.
// using the length returned by your function, it prints the first len elements.
for (int i = 0; i < len; i++) {
  print(nums[i]);
}

1. 简单判断列表中元素是否相等,相等就删除多余元素

def removeDuplicates(self, nums):
    """
    :type nums: List[int]
    :rtype: int
    """
    if not nums:
      return 0
    if len(nums)==1:  #单独判断列表长度为1的情况,因为之后的for循环从下标1开始
      return 1
    temp_num = nums[0]
    count =0     #for循环中动态删除列表元素,列表缩短,为了防止下标溢出需要用count标记删除元素个数
    for index, num in enumerate(nums[1:]):
      if temp_num == num:   #元素相等就删除
        del nums[index-count]
        count += 1
      else:
        temp_num = num
    return len(nums)
def removeDuplicates(self, nums):
    """
    :type nums: List[int]
    :rtype: int
    """
    forth = 0
    back = 1
    while back <= len(nums)-1:
      if nums[forth] == nums[back]:
        nums.pop(back)
      else:
        forth += 1
        back += 1
    return len(nums)

2. 修改数组,保证数组的前几个数字互不相同,且这几个数字的长度同返回长度相等

def removeDuplicates(self, nums):
    """
    :type nums: List[int]
    :rtype: int
    """
    if not nums:
      return 0
    length = 0   #不存在重复数字的数组长度
    for index in range(1,len(nums)):   #遍历数组
      if nums[index] != nums[length]:
        length += 1
        nums[length] = nums[index]
    return length+1

算法题来自:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/description/

Python 相关文章推荐
python使用心得之获得github代码库列表
Jun 25 Python
在Python的Flask框架中使用日期和时间的教程
Apr 21 Python
详细解析Python中的变量的数据类型
May 13 Python
Python中装饰器高级用法详解
Dec 25 Python
python如何去除字符串中不想要的字符
Jul 05 Python
python2.7无法使用pip的解决方法(安装easy_install)
Apr 03 Python
python判断输入日期为第几天的实例
Nov 13 Python
python利用tkinter实现屏保
Jul 30 Python
tensorflow 获取所有variable或tensor的name示例
Jan 04 Python
Python SMTP配置参数并发送邮件
Jun 16 Python
pytorch 如何使用float64训练
May 24 Python
手残删除python之后的补救方法
Jun 26 Python
python重试装饰器的简单实现方法
Jan 31 #Python
Python实现合并两个有序链表的方法示例
Jan 31 #Python
Django 日志配置按日期滚动的方法
Jan 31 #Python
Python类的继承用法示例
Jan 31 #Python
判断python对象是否可调用的三种方式及其区别详解
Jan 31 #Python
python3使用QQ邮箱发送邮件
May 20 #Python
Python实现FTP弱口令扫描器的方法示例
Jan 31 #Python
You might like
解析php中const与define的应用区别
2013/06/18 PHP
Smarty环境配置与使用入门教程
2016/05/11 PHP
Thinkphp批量更新数据的方法汇总
2016/06/29 PHP
Yii框架使用魔术方法实现跨文件调用功能示例
2017/05/20 PHP
不错的新闻标题颜色效果
2006/12/10 Javascript
JavaScript 保存数组到Cookie的代码
2010/04/14 Javascript
Jquery优化效率 提升性能解决方案
2010/09/06 Javascript
js获取url中&quot;?&quot;后面的字串方法
2014/05/15 Javascript
Jquery动态添加及删除页面节点元素示例代码
2014/06/16 Javascript
javascript使用avalon绑定实现checkbox全选
2015/05/06 Javascript
angular select 默认值设置方法
2017/06/23 Javascript
基于Vue实现支持按周切换的日历
2020/09/24 Javascript
实现div滚动条默认最底部以及默认最右边的示例代码
2017/11/15 Javascript
JavaScript解析及序列化JSON的方法实例分析
2019/01/04 Javascript
微信小程序的mpvue框架快速上手指南
2019/05/15 Javascript
[01:51]DAC趣味视频-如何成为职业选手.mp4
2017/04/02 DOTA
python设置windows桌面壁纸的实现代码
2013/01/28 Python
python查看zip包中文件及大小的方法
2015/07/09 Python
使用PyV8在Python爬虫中执行js代码
2017/02/16 Python
Python设计模式之策略模式实例详解
2019/01/21 Python
python 爬虫百度地图的信息界面的实现方法
2019/10/27 Python
匡威意大利官方商店 :Converse意大利
2018/11/27 全球购物
阿迪达斯英国官方网站:adidas英国
2019/08/13 全球购物
中国京东和泰国中央集团合资的网站:JD CENTRAL
2020/08/22 全球购物
护士求职自荐信范文
2014/03/19 职场文书
英语系毕业生求职信
2014/07/13 职场文书
井冈山红色之旅心得体会
2014/10/07 职场文书
2015年九一八事变纪念日演讲稿
2015/03/19 职场文书
2015年度村委会工作总结
2015/04/29 职场文书
2015年女工委工作总结
2015/07/27 职场文书
爱护环境建议书
2015/09/14 职场文书
申论不会写怎么办?教您掌握这6点思维和原则
2019/07/17 职场文书
世界文化遗产导游词
2019/08/07 职场文书
创业计划书之家政服务
2019/09/18 职场文书
Lombok的详细使用及优缺点总结
2021/07/15 Java/Android
Python机器学习应用之基于线性判别模型的分类篇详解
2022/01/18 Python