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. cordova 企业应用打包Archive的时候报 "#import <Cordova file not found"

    可能原因是Cordova的路径问题: For xcode7 add "$(OBJROOT)/UninstalledProducts/$(PLATFORM_NAME)/include" ...

  2. python string.py 源码分析 一

    # Some strings for ctype-style character classification c风格字符串 whitespace = ' \t\n\r\v\f' #空白字符 \t 制 ...

  3. 7.15python进程锁

    #!/usr/bin/env python #!--*--coding:utf-8 --*-- #!@Time :2018/7/14 17:33 #!@Author TrueNewBee #锁 # 火 ...

  4. java中常用的加密方式

    加密,是以某种特殊的算法改变原有的信息数据,使得未授权的用户即使获得了已加密的信息,但因不知解密的方法,仍然无法了解信息的内容.大体上分为双向加密和单向加密,而双向加密又分为对称加密和非对称加密(有些 ...

  5. 02Modify.ashx(修改班级信息)

    02Modify.html 修改 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> & ...

  6. 网络通信协议五之IP协议详解

    网络层协议 >>IP协议 >>ARP(地址解析协议) >>RARP(反向地址解析协议) >>ICMP(互联网控制消息协议) IP协议功能 >> ...

  7. df

    hdu 1052 Tian Ji -- The Horse Racing (2011-08-26 08:32:51) 转载▼ 标签: 杂谈 分类: acm杂谈 Tian Ji -- The Horse ...

  8. day2 二、编程语言、python解释器和变量

    一.编程语言分类 1.机器语言 直接用计算机能理解的二进制指令编写程序,直接控制硬件,需要了解硬件的操作细节. 2.汇编语言 用英文标签取代二进制编写程序,也是直接控制硬件,也需要了解硬件的操作细节. ...

  9. weblogic初学笔记2-在Linux上部署项目

    一.这两天在做部署项目到Linux服务器上. 网上有用war包部署的,也有把war包解压之后部署的.比如:http://www.cnblogs.com/xdp-gacl/p/4143413.html ...

  10. jdbc--------JdbcUtilDao 类

    2018-12-14 目标:做成一个比较通用的 sql 操作 import com.ljs.util.JDBCUtil; 类名:JdbcUtilDao 1: 更新操作, 针对任何表,增加,删除,更新操 ...