[array] leetCode-27. Remove Element - Easy
27. Remove Element - Easy
descrition
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 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.
Example
Given nums = [3,2,2,3], val = 3,
Your function should return length = 2, with the first two elements of nums being 2.
解析
核心思想:双指针;注意需求的分析,根据需求进一步优化设计。
方法 1
参见 code: int removeElementKeepOrder(vector& nums, int val)
快慢指针,icur 指向新数组的结尾,i 遍历数组,只要当前值不等于 val 则进行复制。
方法 2
注意到题目的两个前提条件:(1)数组中元素的位置可以改变;(2)超过最新长度 length 后面的元素无关紧要。
这两个条件使得算法进一步优化成为可能。比如当 nums=[1,2,3,5,4],val=4,如果使用方法 1,将产生 4 次赋值操作。而 nums=[4,1,2,3,5],val=4 时,也将产生 4 次赋值操作,实际上我们可以直接将 4 直接删掉的。
还是两个指针,只不过这是两个对立的指针,icur 从前往后,指向当前要检查的数,在此之前的数都是满足要求的,当 nums[icur] 不满足要求时,值需要将 nums[iend] 赋值给它,并将 iend 自减即可, 这相当于之间删除 val。虽然时间复杂度还是 O(n),但实际上赋值操作的次数只等于需要删除的元素个数,当需要删除的元素个数很少时,算法很高效。
code
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution{
public:
int removeElement(vector<int>& nums, int val){
//return removeElementKeepOrder(nums, val);
return removeElementChangePlace(nums, val);
}
// time-O(n), space-O(1)
int removeElementKeepOrder(vector<int>& nums, int val){
int icur = 0;
for(int i=0; i<nums.size(); i++){
if(nums[i] != val){
nums[icur++] = nums[i];
}
}
return icur; // return new length
}
// time-O(n). In this approach, the number of assignment operation is
// equal to the number of elements to remove. So it is more efficent if
// elements to remove are rare.
// Note: this optimization is based on the condition of
// (1) The order of elements can be changed
// (2) It doesn't matter what you leave beyond the new length.
// space-O(1),
int removeElementChangePlace(vector<int>& nums, int val){
int icur = 0;
int iend = nums.size() - 1;
while(icur<=iend){
if(nums[icur] == val){
// move nums[icur] to the end and delete which operation is equal to subtract iend.
// note: icur can't be decrease, because the current element
// dosen't check
nums[icur] = nums[iend];
iend--;
}else{
icur++;
}
}
return iend+1;
}
};
int main()
{
return 0;
}
[array] leetCode-27. Remove Element - Easy的更多相关文章
- leetCode 27.Remove Element (删除元素) 解题思路和方法
Remove Element Given an array and a value, remove all instances of that value in place and return th ...
- Leetcode 27. Remove Element(too easy)
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 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 27 Remove Element
Problem: Given an array and a value, remove all instances of that value in place and return the new ...
- (Array)27. Remove Element
Given an array and a value, remove all instances of that value in place and return the new length. D ...
- 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 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]27. Remove Element删除元素
Given an array nums and a value val, remove all instances of that value in-place and return the new ...
随机推荐
- unique & lower_bound C++
原来C++也有unique和lower_bound,只需头文件iostream unique unique可以对数组进行相邻元素的"去重",实现效果是把所有不重复的元素按顺序放在数 ...
- spring各个版本开发包下载
spring各个开发包版本下载地址:https://repo.spring.io/webapp/#/artifacts/browse/tree/General/libs-release-local/o ...
- _1Python简介 安装及版本检测
简介 Python是一种面向对象的解释性计算机程序设计语言,由荷兰人Guido von Rossum于1988年的圣诞节发明,第一个公开发行版于1991年. Python崇尚优美.清晰.简单,是一个优 ...
- 验证 Swarm 数据持久性 - 每天5分钟玩转 Docker 容器技术(104)
上一节我们成功将 Rex-Ray Volume 挂载到了 Service.本节验证 Failover 时,数据不会丢失. Scale Up 增加一个副本: docker service update ...
- JAVA8新特性(一)
default拓展方法 java8为接口声明添加非抽象方法的实现,也成为拓展方法. public interface Formula { void doSomething(); default voi ...
- 掌握NIO,程序人生
就像新IO为java带来的革新那样,让我们也开启一段新的程序人生. 关键字:NIO,BIO,伪IO,AIO,多路复用选择器,通道,缓冲区,jdk研究,回调函数,高并发 java.nio 概述 历史背景 ...
- Guice之IOC教程
Guice 在上一篇博客中, 我们讲解了Spring中的IOC示例与实现, 本文着重介绍Guice注入以及与Spring中的差异. Guice是Google开发的, 一个轻量级的依赖注入框架, 跟Sp ...
- eclipse导入新项目后,运行时找不到主类解决办法
最近在学习多线程,今天下了一套源码,导入到eclipse里后,随便找了个带main()的类试了一下,找不到主类. 首先想到的解决办法是把工程clean一下,并没有用.去网上找了一个遍终于找到了管用的方 ...
- Angular之指令Directive系列
项目筹备近期开启Angular学习,指令比较难理解所以记录备案,推荐Angualr实战学习视频大漠穷秋 Angular实战 一.指令directive概述 指令可以对元素绑定事件监听或者改变DOM结构 ...
- css3的动画特效--元素旋转
开发中,视觉要你实现一个元素的旋转问题,比如说如下图所示: 思路:首先动画动效肯定离不开anmimation动画. 和transition动画一样,animation动画也是CSS3动画的一种,这类动 ...