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. wpgcms---导航高亮显示

    在使用wpgcms做项目的时候,有时候三级栏目默认是没有高亮显示的一级导航的,例如:文章详情页要对应的文章栏目进行高亮显示,三级单篇页要对应栏目是高亮显示.具体做法是: 首先看获取导航的方式: {% ...

  2. Linux环境变量与文件查找

    作业: 找出/etc目录下所有以.list结尾的文件 代码:locate /etc/\*.list sudo find /etc/ -name \*.list

  3. 文本分类学习 (十)构造机器学习Libsvm 的C# wrapper(调用c/c++动态链接库)

    前言: 对于SVM的了解,看前辈写的博客加上读论文对于SVM的皮毛知识总算有点了解,比如线性分类器,和求凸二次规划中用到的高等数学知识.然而SVM最核心的地方应该在于核函数和求关于α函数的极值的方法: ...

  4. {03--CSS布局设置} 盒模型 二 padding bode margin 标准文档流 块级元素和行内元素 浮动 margin的用法 文本属性和字体属性 超链接导航栏 background 定位 z-index

    03--CSS布局设置 本节目录 一 盒模型 二 padding(内边距) 三 boder(边框) 四 简单认识一下margin(外边距) 五 标准文档流 六 块级元素和行内元素 七 浮动 八 mar ...

  5. Dapper的数据库连接管理(打开、关闭)

    Dapper对于数据库连接的管理:如果已经打开,它会关闭连接.如果你只是做一个快速查询-让Dopter自己处理它. 如果你做了很多事情,你应该自己打开连接,并在最后关闭连接,所有的查询在中…只是从效率 ...

  6. SmartStore.Net、NopCommerce 全局异常处理、依赖注入、代码研究

    以下是本人最近对NopCommerce和SmartStore.net部分代码的研究和总结,主要集中于:依赖注入.异常处理.对象映射.系统缓存.日志这些方面,供大家参考. NOP 3.8 /// < ...

  7. Vitrual Box设置linux网络连接到外网

    Vitrual Box设置linux网络连接到外网 在虚拟机上安装好linux系统之后,经常会碰到ping不通www.baidu.com的情况,此时的情况多半是网络配置上的错误,linux在网络配置有 ...

  8. NLP去特殊字符

    在自然语言处理中,我们有时对文本进行处理,需要去除一些特殊符号,保留中文,这是在预处理过程中常用到的.分享给你,希望对你有帮助! import re def delete_sysbol(line): ...

  9. angular 使用dialog的经验

    利用angular在近期的工作中使用了dialog的方式,总结下经验 由于dialog显示的内容不同,需要用到angular 的ng-include加载不同的文件1 dialog利用指令的方式 app ...

  10. express链接mysql, 用数据库连接池管理链接

    1.在API的开发当中,数据库的处理显得尤为重要,express 工程 链接mysql数据库有很好的模板可以借鉴. 1.1 创建数据库链接 新建一个DB目录,在DB目录下新建文件 db.js 内容如下 ...