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.

题目地址: Remove Element

难度: Easy

题意: 删除指定值,并将其他元素移动前面

思路:

遍历,双指针

代码:

class Solution(object):
def removeElement(self, nums, val):
"""
:type nums: List[int]
:type val: int
:rtype: int
"""
n = len(nums)
i, j = 0, n-1
while i <= j:
if nums[i] == val:
nums[i], nums[j] = nums[j], nums[i]
j -= 1
else:
i += 1
return i

时间复杂度: O(n)

空间复杂度: O(1)

27. Remove Element@python的更多相关文章

  1. [Leetcode][Python]27: Remove Element

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 27: Remove Elementhttps://oj.leetcode.c ...

  2. 27. Remove Element【leetcode】

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

  3. 27. Remove Element【easy】

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

  4. leetCode练题——27. Remove Element

    1.题目 27. Remove Element——Easy Given an array nums and a value val, remove all instances of that valu ...

  5. C# 写 LeetCode easy #27 Remove Element

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

  6. 【LeetCode】27. Remove Element (2 solutions)

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

  7. leetCode 27.Remove Element (删除元素) 解题思路和方法

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

  8. 【LeetCode】27. Remove Element 解题报告(Python & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 记录起始位置 日期 题目地址:https:/ ...

  9. [LeetCode&Python] Problem 27. Remove Element

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

随机推荐

  1. JAVA编写的断点续传小程序

    上了一周的课,今天终于可以休息了,太棒了,今天闲着无聊使用java语言写了一个断点续传的小程序来分享给大家, 首先要下载个用于网络请求的框架:我这里给出地址,是用的Apache的HttpClient: ...

  2. Ubuntu英文版中无法输入中文标点符号的问题

    问题: 不管是中文还是英文输入法,输入的标点符号都是英文的 解决方法: ctrl + .  进行切换,一个是lation 符号,一个是全角符号

  3. docker 使用数据库mysql

    1. docker pull mysql  获取mysql镜像 2. docker images 查看镜像列表 3.  docker run -itd -P mysql bash :启动mysql镜像 ...

  4. excel 公式2列合并

    =A2&"="&C2 ="UPDATE comm_department SET parent_id='"&D2&"' ...

  5. js json与字符串相互转换

    JSON.parse(jsonstr); //可以将json字符串转换成json对象 JSON.stringify(jsonobj); //可以将json对象转换成json对符串

  6. Luogu P3384 【模板】树链剖分

    ...rt...安利一发大佬博客https://www.cnblogs.com/ivanovcraft/p/9019090.html 注意:不要把dfn和rw弄混了... #include<cs ...

  7. 牛客假日团队赛2 H.奶牛排序

    链接: https://ac.nowcoder.com/acm/contest/924/H 题意: 农夫JOHN准备把他的 N(1 <= N <= 10,000)头牛排队以便于行动.因为脾 ...

  8. CodeForces - 507B - Amr and Pins(计算几何)

    Amr loves Geometry. One day he came up with a very interesting problem. Amr has a circle of radius r ...

  9. 破解百度翻译页面api参数加密

    我们的目标 https://fanyi.baidu.com/      找到获取翻译的请求 是这个 https://fanyi.baidu.com/v2transapi 查看一下post提交的表单,是 ...

  10. HDU 5875 H - Function 用单调栈水过了

    http://acm.hdu.edu.cn/showproblem.php?pid=5875 单调栈,预处理to[i]表示第一个比a[i]小的数字,一直跳就可以. 这题是数据水而已. 这里学习下单调栈 ...