Given an array nums and a value val, remove all instances of that value in-place 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. The order of elements can be changed. It doesn't matter what you leave beyond the new length.

Example 1:   Given nums = [3,2,2,3], val = 3

Your function should return length = 2, with the first two elements of nums being 2.It doesn't matter what you leave beyond the returned length.

Example 2:   Given nums = [0,1,2,2,3,0,4,2], val = 2,

      Your function should return length = 5, with the first five elements of nums containing 0, 1, 3, 0, and 4.Note that the order of those five elements can be arbitrary.           It doesn't matter what values are set beyond the returned length.

思路

  因为我经常使用python解题,当我看到这道题之后使用python中列表自带的pop()方法来解决的,就是从头遍历到尾,如果相等的元素我们就直接弹出。遍历到尾部结束。时间复杂度为O(n),空间复杂度为O(1)。
  如果我们不使用pop()方法,有没有其他方法可以解决?我想到了两个指针的方法分别指向头和尾,如果遇到和val相等的元素将尾指针指向的元素赋值到头指针的位置。直到头和尾指针相遇结束。时间复杂度为O(n), 空间复杂度为O(1).
第二种思路图示


第一种思路解决代码


 class Solution(object):
def removeElement(self, nums, val):
"""
:type nums: List[int]
:type val: int
:rtype: int
"""
if len(nums) < 1: # 没有元素直接弹出
return 0
i,leng = 0, len(nums) while i < len(nums):
if nums[i] == val: # 相等就弹出,
nums.pop(i)
leng -= 1
else:
i +=1
return len(nums)
第二种思路解决代码


 class Solution(object):
def removeElement(self, nums, val):
"""
:type nums: List[int]
:type val: int
:rtype: int
"""
if len(nums) < 1:
return 0
i,leng = 0, len(nums)-1
while i <= leng:
if nums[i] == val:
nums[i] = nums[leng] # 将尾部的元素进行赋值。
leng -= 1
else:
i+= 1
return leng+1

【LeetCode每天一题】Remove Element(移除指定的元素)的更多相关文章

  1. leetcode第25题--Remove Element

    problem: Given an array and a value, remove all instances of that value in place and return the new ...

  2. 【转载】C#中List集合使用Remove方法移除指定的对象

    在C#的List集合操作中,有时候需要将特定的对象或者元素移除出List集合序列中,此时可使用到List集合的Remove方法,Remove方法的方法签名为bool Remove(T item),it ...

  3. 【python】Leetcode每日一题-删除排序链表中的重复元素

    [python]Leetcode每日一题-删除排序链表中的重复元素 [题目描述] 存在一个按升序排列的链表,给你这个链表的头节点 head ,请你删除所有重复的元素,使每个元素 只出现一次 . 返回同 ...

  4. 【python】Leetcode每日一题-删除排序链表中的重复元素2

    [python]Leetcode每日一题-删除排序链表中的重复元素2 [题目描述] 存在一个按升序排列的链表,给你这个链表的头节点 head ,请你删除链表中所有存在数字重复情况的节点,只保留原始链表 ...

  5. 【LeetCode算法-27】Remove Element

    LeetCode第27题 Given an array nums and a value val, remove all instances of that value in-place and re ...

  6. Leetcode 题目整理-7 Remove Element & Implement strStr()

    27. Remove Element Given an array and a value, remove all instances of that value in place and retur ...

  7. [LeetCode] Remove Element 移除元素

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  8. [LeetCode] 27. Remove Element 移除元素

    Given an array nums and a value val, remove all instances of that value in-place and return the new ...

  9. 【算法】LeetCode算法题-Remove Element

    这是悦乐书的第150次更新,第152篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第9题(顺位题号是27).给定整数数组nums和值val,删除nums中所有的val值, ...

随机推荐

  1. 10.17正式开发stark项目(二)

    2018-10-17 11:09:48 orm补充参考连接: https://www.cnblogs.com/yuanchenqi/articles/8963244.html model 进阶 参考连 ...

  2. 6.26 py GIL

    在python中,多进程效率远大于多线程效率 python中存在GIL这个"线程锁", 关键地方可以使用c语言解决  GIL问题  然后可以提高cpu占用效率 异步的实现!!! 同 ...

  3. ztree 文件夹类型的 树状图

    未套程序的源代码: 链接:http://pan.baidu.com/s/1nuHbxhf 密码:4aw2 已套程序的源代码: css样式: /*发布邮件 选择领导弹窗*/ .xuandao{ disp ...

  4. jquery <img> 图片懒加载 和 标签如果没有加载出图片或没有图片,就显示默认的图片

    参考链接:http://www.jq22.com/jquery-info390 或压缩包下载地址:链接:http://pan.baidu.com/s/1hsj8ZWw 密码:4a7s    下面是没有 ...

  5. 解决css设置背景透明,文字不透明

    设置元素的透明度:  -moz-opacity:0.8; /*在Firefox中设置元素透明度  filter: alpha(opacity=80); /*ie使用滤镜设置透明   但是当我们对一个标 ...

  6. bootstrap-switch 使用

    网址:http://www.bootcss.com/p/bootstrap-switch/ 界面设置不调用方法没成功,事件也不起作用不知道是jquery版本原因还是什么原因!,下面亲测试可以使用 $( ...

  7. 阿里云不同账号之间相同地域的VPC网络互访

    今天实际操作了一下,在这篇随笔中记录一下以备忘,主要参考阿里云帮助文档-不同账号下专有网络内网互通. 实现场景:账号A的VPC网络中的ECS访问账号B的VPC网络中的ECS与RDS(地域都在华东1), ...

  8. {前端CSS} 语法 Css的几种引入方式 css选择器 选择器的优先级 CSS属性相关 背景属性 边框 CSS盒子模型 清除浮动 overflow溢出属性  定位(position)z-index

    前端CSS CSS介绍 CSS(Cascading Style Sheet,层叠样式表)定义如何显示HTML元素,给HTML设置样式,让它更加美观. 当浏览器读到一个样式表,它就会按照这个样式表来对文 ...

  9. mapReducer第一个例子WordCount

    mapreducer第一个例子,主要是统计一个目录下各个文件中各个单词出现的次数. mapper package com.mapreduce.wordCount; import java.io.IOE ...

  10. 树和二叉树->线索二叉树

    文字描述 从二叉树的遍历可知,遍历二叉树的输出结果可看成一个线性队列,使得每个结点(除第一个和最后一个外)在这个线形队列中有且仅有一个前驱和一个后继.但是当采用二叉链表作为二叉树的存储结构时,只能得到 ...