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的更多相关文章

  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 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 ...

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

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

  4. [LeetCode] 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

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

  6. (Array)27. Remove Element

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

  7. Java [leetcode 27]Remove Element

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

  8. Leetcode 27——Remove Element

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

  9. (双指针) leetcode 27. Remove Element

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

  10. [leetcode]27. Remove Element删除元素

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

随机推荐

  1. python3学习笔记(2)

    一.面向对象(初识)由类和方法组成,类里面封装了很多功能,根据这个类,可以创建一个这个类的对象,即对象是根据这个类创建的,以后这个对象要使用某个功能的时候就从这个类里面的找.例:str -功能一 -功 ...

  2. 魔方NewLife.Cube升级v2.0

    魔方是一套集成权限管理的MVC管理后台,最具特色功能是模版覆盖机制,是XCode实体类的最佳搭档! v2.0.2017.1126   借助Ajax支持高级操作,如:删除选中.批量启用禁用等 用户管理增 ...

  3. 在没有DOM操作的日子里,我是怎么熬过来的(中)

    前言 继上篇推送之后,在掘金.segmentfault.简书.博客园等平台上迅速收到了不俗的反馈,大部分网友都留言说感同身受,还有不少网友追问中篇何时更新.于是,闰土顺应呼声,在这个凛冽的寒冬早晨,将 ...

  4. python使用rsa库做公钥解密(网上别处找不到)

    使用RSA公钥解密,用openssl命令就是openssl rsautl -verify -in cipher_text -inkey public.pem -pubin -out clear_tex ...

  5. UnityShader-菲涅尔反射(Fresnel Reflection)

    菲涅耳公式(或菲涅耳方程),由奥古斯丁·让·菲涅耳导出.用来描述光在不同折射率的介质之间的行为.由公式推导出的光的反射称之为"菲涅尔反射".菲涅尔公式是光学中的重要公式,用它能解释 ...

  6. PHP入门怎么选?大学生适合学习吗?

    大学毕业,面对竞争激烈的社会,理想总是很丰满,现实却很残酷.在硕士.博士都随处可见的今天,本科和大专文凭就显得苍白无力,在面试官问你"有没有工作经验"的时候,你是不是只想起实习期间 ...

  7. lua 限流

    前言 每逢大促必压测,每逢大促必限流,这估计是电商人的常态.每次大促期间,业务流量是平时的几倍十几倍,大促期间大部分业务都会集中在购物车结算,必须限流,才能保证系统不宕机. 限流算法 限流算法一般有三 ...

  8. SQL Server学习之路(六):“增删改查”之“查”

    0.目录 1.前言 2.最基本的SQL查询语句 3.select...from... 3.1 "*"与"Top num *" 3.2 查询指定列 3.3 Isn ...

  9. bootstrap validator 使用 带代码

    如何使用bootstrapVlidator插件? 下载bootstrapVlidator插件 在需要使用的页面引入bootstrapVlidator的js文件和css文件 如: 注: 在此基础之前必须 ...

  10. js比较日期大小

    第一种方法: var starttime = "2007-1-2 7:30"; var endtime = "2007-2-31 8:30"; alert(Co ...