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 -version 命令,如下图所示如果没有明确显示位数的,则说明是32位 C:\MyTools\jdk1.7.0\bin>java -version java version & ...

  2. CodeForces - 296A-Yaroslav and Permutations(思维)

    Yaroslav has an array that consists of n integers. In one second Yaroslav can swap two neighboring a ...

  3. 如何使用在Windows 下AspNetCore Api 和 consul

    在Windows 下如何使用 AspNetCore Api 和 consul https://blog.csdn.net/sD7O95O/article/details/80750803 一.概念:什 ...

  4. NET Core+MySql+Nginx

    NET Core+MySql+Nginx 容器化部署 .NET Core容器化@Docker.NET Core容器化之多容器应用部署@Docker-Compose.NET Core+MySql+Ngi ...

  5. Canada Cup 2016 D. Contest Balloons 好题。优先队列 + 简单贪心

    http://codeforces.com/contest/725/problem/D 这题一看就是贪心的了,w - t最小的那个,肯定是优先打死. 但是一直都不会写,为什么呢,因为这个太像二分答案了 ...

  6. (转)sudo配置文件/etc/sudoers详解及实战用法

    sudo配置文件/etc/sudoers详解及实战用法 原文:http://blog.csdn.net/field_yang/article/details/51547804 一.sudo执行命令的流 ...

  7. (转)Linux日志管理+ last lastlog lastb

    Linux日志管理+ last lastlog lastb 原文:http://blog.csdn.net/xin_y/article/details/53440707 日志管理 日志通常存放在 /v ...

  8. 写TXT文件

    #region 写日志 private static void writelog(string strwrite) { string strPath = "d:/log.txt"; ...

  9. MySQL 查看表大小

    当遇到数据库占用空间很大的情况下,可以用以下语句查找大数据量的表 SELECT TABLE_NAME ,),) 'DATA_SIZE(M)' ,),) 'INDEX_SIZE(M)' ,AVG_ROW ...

  10. 解决ueditor jquery javascript 取值问题

    代码如下: var content = UE.getEditor('myEditor').getContent();   myEditor是ueditor 的名称name.   代码如下: <t ...