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. [转]the service mysql57 failed the most recent status[/br]mysql57 was not found解决办法

    转自:http://forums.mysql.com/read.php?169,622722,622877#msg-622877 安装完mysql5.7.12后想要stop或者restart都会出现以 ...

  2. [最短路]P1462 通往奥格瑞玛的道路

    题目背景 在艾泽拉斯大陆上有一位名叫歪嘴哦的神奇术士,他是部落的中坚力量 有一天他醒来后发现自己居然到了联盟的主城暴风城 在被众多联盟的士兵攻击后,他决定逃回自己的家乡奥格瑞玛 题目描述 在艾泽拉斯, ...

  3. 操作系统学习笔记----进程/线程模型----Coursera课程笔记

    操作系统学习笔记----进程/线程模型----Coursera课程笔记 进程/线程模型 0. 概述 0.1 进程模型 多道程序设计 进程的概念.进程控制块 进程状态及转换.进程队列 进程控制----进 ...

  4. Java 将两个有序数组合成为一个有序数组

    基本思路 1.如果其中一个数组的元素均大于另一个数组的元素,则可以直接组合,不用拆分. 即:其中一个数组的第一个元素大于或者小于另一个数组的最后一个元素 2.若不满足1中的情况,则表明数组需要拆分,拆 ...

  5. 用lua+redis实现一个简单的计数器功能 (一)

    首先安装环境 依赖环境有 luajit http://luajit.org ngx_devel_kit https://github.com/simpl/ngx_devel_kit echo-ngin ...

  6. [LeetCode] 74 Search a 2D Matrix(二分查找)

    二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...

  7. SpringMVC集成Shiro、读取数据库操作权限

    1.Maven添加Shiro所需的jar包 <dependency> <groupId>org.apache.shiro</groupId> <artifac ...

  8. npm install 时报错 Unexpected end of input at 1:15930

    从github上clone代码后npm install,结果解决办法: npm config set registry https://registry.npm.taobao.org之后出现自动生成了 ...

  9. codeforces 893B Beautiful Divisors 打表

    893B Beautiful Divisors 思路: 打表 代码: #include <bits/stdc++.h> using namespace std; #define _for( ...

  10. 深入理解php内核 编写扩展 II:参数、数组和ZVALs

    原文:http://devzone.zend.com/article/1022-Extension-Writing-Part-II-Parameters-Arrays-and-ZVALs Part I ...