作者: 负雪明烛
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. perl和python3 同时打开两个文件

    perl : while(defined(my $f1=<FQ1>) && defined(my $f2=<FQ2>)){ condition } python ...

  2. 学习java的第十六天

    一.今日收获 1.完成了手册第二章没有验证完成的例题 2.预习了第三章的算法以及for语句与if语句的用法 二.今日难题 1.验证上出现问题,没有那么仔细. 2.第二章还有没有完全理解的问题 三.明日 ...

  3. 学习java 6.30

    学习内容:Java的运算符与C中类似,虽是类似,还是有点区别,在这里详细说明一下,即字符以及字符串的+操作,字符的+操作执行后需要赋值给表达式中数据范围最大的类型, 字符串的+操作,当+中有字符串,则 ...

  4. 零基础学习java------23---------动态代理,ip,url案例

    1. 动态代理 2. ip,url案例 给定的access.log是电信运营商的用户上网数据,第一个字段是时间, 第二个字段是ip地址,第三个字段是访问的网站,其他字段可以忽略不计. 第一个字段是网段 ...

  5. Output of C++ Program | Set 7

    Predict the output of following C++ programs. Question 1 1 class Test1 2 { 3 int y; 4 }; 5 6 class T ...

  6. zabbix之监控面试

    先用shell脚本把值取出来,然后重启agent,在server端用zabbix-get命令测试一下,看能不能通过userparameter指定的可以将值取出来,如果没问题,在在网页创建模板,加监控项 ...

  7. 为Python的web框架编写前端模版的教程

    虽然我们跑通了一个最简单的MVC,但是页面效果肯定不会让人满意. 对于复杂的HTML前端页面来说,我们需要一套基础的CSS框架来完成页面布局和基本样式.另外,jQuery作为操作DOM的JavaScr ...

  8. spring mvc idea创建

    创建项目 创建项目 --> Spring --> Spring MVC --> 下面选择Download,会显示Spring MVC-5版本 如果是首次使用IDEA,因为没有配置ma ...

  9. 在Eclipse中运行OSGI工程出错的解决方案

    今天学习OSGI的过程中按照书上所述搭建好第一个helloworld插件工程,运行的过程中出现下面所示的错误: !SESSION 2014-06-09 21:04:49.038 ----------- ...

  10. VFL

    VFL 1. 概念 VFL全称是Visual Format Language,翻译过来是"可视化格式语言" VFL是苹果公司为了简化Autolayout的编码而推出的抽象语言 2. ...