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. 从CentOS安装完成到生成词云python学习日记

    欢迎访问我的个人博客:原文链接 前言 人生苦短,我用python.学习python怎么能不搞一下词云呢是不是(ง •̀_•́)ง 于是便有了这篇边实践边记录的笔记. 环境:VMware 12pro + ...

  2. MVC Code First(数据模型实例讲解)

    首先配置好web.config <connectionStrings> <add name="BookDbContext" connectionString=&q ...

  3. 状态机编程思想(2):删除代码注释(目前支持C/C++和Java)

    有时为了信息保密或是单纯阅读代码,我们常常需要删除注释. 之前考虑过正则表达式,但是感觉实现起来相当麻烦.而状态机可以把多种情况归为一类状态再行分解,大大简化问题.本文就是基于状态机实现的. 删除C/ ...

  4. Codeforces543BDestory Roads心得

    题目描述: In some country there are exactly n cities and m bidirectional roads connecting the cities. Ci ...

  5. IPv6 VS IPv4,谈谈升级 IPv6 的必要性

    11月26日,中办.国办印发了<推进互联网协议第六版(IPv6)规模部署行动计划>,提出国内要在 5~10 年的时间形成下一代互联网自主技术体系和产业生态,建成全球最大规模的 IPv6 商 ...

  6. 小程序采坑系列-this.setData

    今天踩了大坑,坑里还都是碎瓶渣子.. 先说一下基本使用.官网也有. 比如说你在main.js里面有这些变量.想修改某些值. data: { main_view_bgcolor: "" ...

  7. 显示mysql线程和kill线程的命令

    show processlist;//显示哪些线程正在运行. kill id //kill线程   通常在表被锁的时候用.   show processlist;显示哪些线程正在运行.您也可以使用my ...

  8. 查找算法(Java实现)

    1.二分查找算法 package other; public class BinarySearch { /* * 循环实现二分查找算法arr 已排好序的数组x 需要查找的数-1 无法查到数据 */ p ...

  9. Kotlin 一个好用的新功能:Parcelize

    在开发中,如果有需要用到序列化和反序列化的操作,就会用到 Serializable 或者 Parcelable,它们各有优缺点,会适用于不同的场景. Serializable 的优点是实现简单,你只需 ...

  10. Python2/3中的urllib库

    urllib库对照速查表 Python2.X Python3.X urllib urllib.request, urllib.error, urllib.parse urllib2 urllib.re ...