【LeetCode】27. Remove Element 解题报告(Python & Java)
作者: 负雪明烛
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)的更多相关文章
- leetCode 27.Remove Element (删除元素) 解题思路和方法
Remove Element Given an array and a value, remove all instances of that value in place and return th ...
- 【LeetCode】169. Majority Element 解题报告(Java & Python & C+)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 思路 hashmap统计次数 摩尔投票法 Moore ...
- Java [leetcode 27]Remove Element
题目描述: Given an array and a value, remove all instances of that value in place and return the new len ...
- 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 ...
- LeetCode 27. Remove Element (移除元素)
Given an array and a value, remove all instances of that value in place and return the new length. D ...
- [LeetCode] 27. Remove Element 移除元素
Given an array nums and a value val, remove all instances of that value in-place and return the new ...
- LeetCode 169 Majority Element 解题报告
题目要求 Given an array of size n, find the majority element. The majority element is the element that a ...
- LeetCode: Find Peak Element 解题报告
Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...
- LeetCode 27 Remove Element (移除数组中指定元素)
题目链接: https://leetcode.com/problems/remove-element/?tab=Description Problem : 移除数组中给定target的元素,返回剩 ...
随机推荐
- R语言与医学统计图形-【12】ggplot2几何对象之条图
ggplot2绘图系统--几何对象之条图(包括误差条图) 1.条图 格式: geom_bar(mapping = , data = , stat = 'count', #统计变换默认计数 positi ...
- mvc中常见的属性验证
客户端验证逻辑会对用户向表单输入的数据给出一个即时反馈.而之所以需要服务器端验证,是因为来自网络的信息都是不能被信任的. 当在ASP.NET MVC设计模式上下文中谈论验证时,主要关注的是验证模型的值 ...
- 02 Windows安装C语言开发工具CodeBlocks
CodeBlocks安装 使用微信扫码关注微信公众号,并回复:"C语言环境",免费获取下载链接! 1.卸载CodeBlocks(电脑未装此软件,跳过) 进入目录:C:\Pro ...
- Spark相关知识点(一)
spark工作机制,哪些角色,作用. spark yarn模式下的cluster模式和client模式有什么区别.
- org.apache.hadoop.hive.ql.metadata.HiveException: Internal Error: cannot generate all output rows for a Partition解决
自己在路径访问明细表开发时,写的sql如下 SELECT guid, sessionid, event['url'] as page, `timestamp` as ts, row_number() ...
- 【JavaWeb安全】RMI-Remote Method Invocator
RMI-Remote Method Invocator 什么是RMI?RMI有什么用? RMI允许用户通过数据传输,调用远程方法,在远程服务器处理数据.例如将1,3传到远程服务器的加法运算器,加法运算 ...
- Ganglia 简单介绍与安装
文章来至于 http://sachinsharm.wordpress.com/2013/08/17/setup-and-configure-ganglia-3-6-on-centosrhel-6- ...
- 转 Android Lifecycle、ViewModel和LiveData
转自:https://www.jianshu.com/p/982545e01d0a 1.概述 在I / O '17的时候,其中一个重要的主题是Architecture Components.这是一个官 ...
- rust常用技巧
允许未使用的方法,写在文件开头,可过滤过掉该项提示 #![allow(unused)]
- 12.Vue.js 表单
这节我们为大家介绍 Vue.js 表单上的应用. 你可以用 v-model 指令在表单控件元素上创建双向数据绑定. <div id="app"> <p>in ...