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. Mysql相关知识点总结(一)

    information_schema库:information_schema库中的表大都是temporory表,都是只读的,不能进行更新.删除和插入等操作,也不能加触发器,因为它们实际只是一个视图,不 ...

  2. kafka 集群搭建

    环境:ubuntu14.04 版本:jdk1.8,zookeeper 3.4.10,kafka 2.11 搭建步骤: 1. 搭建zookeeper集群 参考链接:zookeeper集群搭建 2. 下载 ...

  3. C++ sqlite3解决中文排序问题

    导言:sqlite3默认的编码方式为UTF8编码,而在UTF8编码下,中文不是按照拼音顺序编码的,所以想解决中文排序问题,必须自定义排序规则,将UTF8编码转换成GB2312编码(GB2312编码中文 ...

  4. fetch知识点汇总

    使用XHR发送一个json请求一般是这样: const xhr = new XMLHttpRequest() xhr.open('Get', url) xhr.responseType = 'json ...

  5. android动画基础之Animation

    android 动画 摘要: 概述 最近总结一下Android的一些东西,毕竟基础不牢地动山摇.本篇主要涉及Animation,对Tween和Frame动画做些总结. Tween Tween动画即补间 ...

  6. 当使用javac编译源文件时,如何查找import导入的类

    当编写一个java源代码文件时,此文件通常被称为编译单元(有时也被称为转移单元).每个编译单元都必须有一个后缀名.java,而在编译单元内则可以有一个public类,该类的名称必须与文件名称一致.每个 ...

  7. [C#]使用Process的StandardInput与StandardOutput写入读取控制台数据

    本文为原创文章.源代码为原创代码,如转载/复制,请在网页/代码处明显位置标明原文名称.作者及网址,谢谢! 开发工具:VS2017 语言:C# DotNet版本:.Net FrameWork 4.0及以 ...

  8. Nginx配置优化及深入讲解,大家可以听一下

    随着访问量的不断增加,需要对Nginx和内核做相应的优化来满足高并发用户的访问,那下面在单台Nginx服务器来优化相关参数. 1)       Nginx.conf配置优化: worker_proce ...

  9. Python 冒泡法排序

    def sequence(disorder='', separators=''): arrays = disorder.split(separators) def desc(): for i in r ...

  10. 【epubcfi函数generateRangeFromCfi和generateCfiFromRange】两者的区别和适用性,以及另一种实现

    epubcfi是描述epub规范电子书中文本位置的一种描述符,它是形如" epubcfi(/6/4[Section0017.xhtml]!4/42/178/1:0,4/42/198/1:1) ...