【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的元素,返回剩 ...
随机推荐
- 13.Merge k Sorted Lists
思路:利用map<int,vector<ListNode*> > 做值和指针的映射,最后将指针按值依次链接起来, 时间复杂度O(N),空间O(N) Merge k sorted ...
- dubbo 协议的 K8s pod 存活探针配置
背景 某项目采用微服务架构,dubbo 框架,K8s 方式部署. 其中 HTTP 协议由网关应用统一处理,大部分应用仅提供 dubbo 协议. 目标 应用某个实例(pod)状态异常时,尝试自动重启恢复 ...
- Hadoop RPC通信
Remote Procedure Call(简称RPC):远程过程调用协议 1. 通过网络从远程计算机程序上请求服务 2. 不需要了解底层网络技术的协议(假定某些传输协议的存在,如TCP或UDP) 3 ...
- 11-如何通过newman生成不同类型的测试报告
postman生成测试报告需要一个插件:newman ,并且这个插件需要先安装 . 安装步骤: 安装nodejs: newman是由nodejs开发,所以要先安装它的运行环境,下载地址:http:// ...
- 【分布式】Zookeeper客户端基本的使用
与mysql.redis等软件一样,zookeeper的软件包中也提供了客户端程序用于对服务器上的数据进行操作.本节我们就来学习zookeeper客户端的使用方法.不过在详细讲解zk客户端的使用方法之 ...
- swift设置导航栏item颜色和状态栏颜色
//swift设置导航栏item颜色和状态栏颜色 let dict:Dictionary =[NSForegroundColorAttributeName:UIColor.hrgb("333 ...
- SpringMVC注解详情
@Component.@Repository @Service.@Controller 看字面含义,很容易却别出其中三个: @Controller 控制层,就是我们的action层 @Service ...
- 优化 if-else 代码的 8 种方案
前言 代码中如果if-else比较多,阅读起来比较困难,维护起来也比较困难,很容易出bug,接下来,本文将介绍优化if-else代码的八种方案. 方案. 优化方案一:提前return,去除不必要的el ...
- spring-dm 一个简单的实例
spring-dm2.0 运行环境,支持JSP页面 运行spring web 项目需要引用包
- feignclient发送get请求,传递参数为对象
feignclient发送get请求,传递参数为对象.此时不能使用在地址栏传递参数的方式,需要将参数放到请求体中. 第一步: 修改application.yml中配置feign发送请求使用apache ...