作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/remove-element/

Total Accepted: 115693 Total Submissions: 341766 Difficulty: Easy

题目描述

Given an array and a value, 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 in place with constant memory.

The order of elements can be changed. It doesn’t matter what you leave beyond the new length.

Example:

Given input array nums = ``[3,2,2,3]``, val = ``3``

Your function should return length = 2, with the first two elements of nums being 2.

题目大意

在原地去除数组中等于val的所有数字,返回的是数组要保留的之前位置的索引。

解题方法

双指针

一个指向前面等于val的数字,一个指向后面不等于val的数字,交换后移动的方式就是交换之后把末尾的指针前移;如果不进行交换操作则把前指针后移。

时间复杂度是O(N),空间复杂度是O(1).

java版本。

public class Solution {
public int removeElement(int[] nums, int val) {
int head=0;
int tail=nums.length;
while(head!=tail){
if(nums[head]==val){
nums[head]=nums[tail-1];
tail--;
}else{
head++;
}
}
return tail;
}
}

AC:1ms

Python 版本。

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

也可以下面这么写,只不过需要加一个判断即当l>r的时候break掉。

class Solution(object):
def removeElement(self, nums, val):
"""
:type nums: List[int]
:type val: int
:rtype: int
"""
N = len(nums)
l, r = 0, N - 1
while l <= r:
while r >= 0 and nums[r] == val:
r -= 1
if l > r:
break
if nums[l] == val:
nums[l], nums[r] = nums[r], nums[l]
l += 1
return l

记录起始位置

这个做法和双指针不一样的地方在于,这个做法使用了begin指针保存的是已经确定了没有val的数组范围,使用for循环来向后遍历查找不等于val的数字放入begin位置。这样确实做了很多无用功,即对出去了等于val的每个数字都做了一次赋值操作。

时间复杂度是O(N),空间复杂度是O(1).

class Solution(object):
def removeElement(self, nums, val):
"""
:type nums: List[int]
:type val: int
:rtype: int
"""
N = len(nums)
begin = 0
for i in range(N):
if nums[i] != val:
nums[begin] = nums[i]
begin += 1
return begin

日期

2016/5/3 11:29:08
2018 年 10 月 31 日 ——十月最后一天,万圣节!

【LeetCode】27. Remove Element 解题报告(Python & Java)的更多相关文章

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

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

  2. 【LeetCode】169. Majority Element 解题报告(Java & Python & C+)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 思路 hashmap统计次数 摩尔投票法 Moore ...

  3. Java [leetcode 27]Remove Element

    题目描述: Given an array and a value, remove all instances of that value in place and return the new len ...

  4. LeetCode Array Easy 27. Remove Element 解题

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

  5. LeetCode 27. Remove Element (移除元素)

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

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

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

  7. LeetCode 169 Majority Element 解题报告

    题目要求 Given an array of size n, find the majority element. The majority element is the element that a ...

  8. LeetCode: Find Peak Element 解题报告

    Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...

  9. LeetCode 27 Remove Element (移除数组中指定元素)

    题目链接: https://leetcode.com/problems/remove-element/?tab=Description   Problem : 移除数组中给定target的元素,返回剩 ...

随机推荐

  1. R语言与医学统计图形-【13】ggplot2几何对象之盒形图

    ggplot2绘图系统--几何对象之盒形图 参数: geom_boxplot(mapping = , #lower,middle,upper,x,ymax,ymin必须(有默认) #alpha/col ...

  2. Docker实用命令介绍

    Docker实用命令介绍 1. docker启动.关闭.停止 ╭─wil-xz in ~ 12:15:44 ╰─٩(ŏ﹏ŏ.)۶ service docker restart Redirecting ...

  3. docker_清华源国内的选择

    清华大学开源镜像官网:https://mirrors.tuna.tsinghua.edu.cn/ 前期: 在centos7 中的extras 源里面有docker 安装包,但是里面的安装包是比较旧的 ...

  4. A Child's History of England.18

    But, although she was a gentle lady, in all things worthy to be beloved - good, beautiful, sensible, ...

  5. 基于 vue-cli 的 lib-flexible 适配

    基于 vue-cli3.0 的 lib-flexible 适配方案 第一步:下载安装相关依赖 第二步:创建 vue.config.js 文件并配置 第三步:在 main.js 中引入 lib-flex ...

  6. 从jvm字节码指令看i=i++和i=++i的区别

    1. 场景的产生 先来看下下面代码展示的两个场景 @Testvoid testIPP() { int i = 0; for (int j = 0; j < 10; j++) { i = i++; ...

  7. oracle extract

    select extract(year from systimestamp) year         ,extract(month from systimestamp) month          ...

  8. Linux lvm在线扩容

    1.查看磁盘空间 [root@bgd-mysql3 ~]# fdisk -l Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sector ...

  9. swagger文档

    关键配置文件 spring boot demo pom.xml <?xml version="1.0" encoding="UTF-8"?> < ...

  10. YC-Framework版本更新:V1.0.2

    分布式微服务框架:YC-Framework版本更新V1.0.2!!! 版本更新,本次版本为V1.0.2 主要更新内容如下所示: 集成ActiveMQ; 集成微信生态(支持微信公众号.微信支付.微信开放 ...