(双指针) leetcode 27. Remove Element
Given an array nums and a value val, 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 1:
Given nums = [3,2,2,3], val = 3, Your function should return length = 2, with the first two elements of nums being 2. It doesn't matter what you leave beyond the returned length.
Example 2:
Given nums = [0,1,2,2,3,0,4,2], val = 2, Your function should return length =5
, with the first five elements ofnums
containing0
,1
,3
,0
, and 4. Note that the order of those five elements can be arbitrary. It doesn't matter what values are set beyond the returned length.
Clarification:
Confused why the returned value is an integer but your answer is an array?
Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.
Internally you can think of this:
// nums is passed in by reference. (i.e., without making a copy)
int len = removeElement(nums, val); // any modification to nums in your function would be known by the caller.
// using the length returned by your function, it prints the first len elements.
for (int i = 0; i < len; i++) {
print(nums[i]);
}
------------------------------------------------------------------------------------------------------------------------------------
这个题如果没有说明"in place" 这个条件的话,也许会更容易,但是,这个已经说明要原地修改数组,也就是要空间复杂度为O(1),于是,我们可以用双指针。我们可以用一个慢指针和快指针,快指针用来从头到尾依次遍历这个原数组,然后用当遍历到不是val的数时,这个数组对应的慢指针指向的索引的数更新,慢指针加一。
C++代码:
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int k = ;
for(int i = ; i < nums.size(); i++){
if(nums[i] != val){
nums[k] = nums[i];
k++;
}
}
return k;
}
};
(双指针) leetcode 27. Remove Element的更多相关文章
- 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 (移除元素)
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 ...
- Leetcode 27 Remove Element STL
和remove zero类似的方法完成该题 class Solution { public: int removeElement(vector<int>& nums, int va ...
- 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(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 nums and a value val, remove all instances of that value in-place and return the new ...
随机推荐
- elementUi的时间选择器在IE浏览器的赋值问题--前端
项目技术:vue+elementUi,组件化 出现的问题:在IE浏览器(IE10+),唤醒组件加载赋值,表单中el-input等框赋值正确,el-date-picker选择器没有显示所附的值(或显示p ...
- EclipseAndroid打包签名发布安装失败闪退运行不了
EclipseAndroid打包签名发布安装失败闪退运行不了 本来没怎么用过用Eclipse写安卓,可是有人有需要必须用Eclipse写,那就写呗. 可在签名打包的时候,发到手机上安装,提示安装成功. ...
- GZIP压缩与解压
public class GZIP { /** * 字符串的压缩 * * @param str * 待压缩的字符串 * @return 返回压缩后的字符串 * @throws IOException ...
- Python第十三天 django 1.6 导入模板 定义数据模型 访问数据库 GET和POST方法 SimpleCMDB项目 urllib模块 urllib2模块 httplib模块 django和web服务器整合 wsgi模块 gunicorn模块
Python第十三天 django 1.6 导入模板 定义数据模型 访问数据库 GET和POST方法 SimpleCMDB项目 urllib模块 urllib2模块 ...
- Neo4j 全文检索
全文检索基本概念 搜索 搜索这个行为是用户与搜索引擎的一次交互过程,用户需要找一些数据,他提供给搜索引擎一些约束条件.搜索引擎通过约束条件抽取一些结果给用户 搜索引擎 搜索引擎存在的目的是存储,查找和 ...
- 本地部署JAVA SE环境
一.下载安装JDK: 下载地址:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-doc-downloads-2133158. ...
- vmware 6 虚拟化 全系列 序列号
vmware 6 虚拟化 全系列 序列号 vSphere 6 Hypervisor HY0XH-D508H-081U8-JA2GH-CCUM2 4C4WK-8KH8L-H85J0-UHCNK-8C ...
- 网络流 之 P2766 最长不下降子序列问题
题目描述 «问题描述: 给定正整数序列x1,...,xn . (1)计算其最长不下降子序列的长度s. (2)计算从给定的序列中最多可取出多少个长度为s的不下降子序列. (3)如果允许在取出的序列中多次 ...
- SQL COUNT() 函数
COUNT() 函数返回匹配指定条件的行数. SQL COUNT() 语法 SQL COUNT(column_name) 语法 COUNT(column_name) 函数返回指定列的值的数目(NULL ...
- 作业MyCP中无法命令行输入的问题解决
问题 上网搜了好久,发现是我当时安装JDK时安装了多个版本的JDK javac -version.java -version发现版本不一样 解决 删掉多余的JDK,并在环境变量Path中找到目录,删掉 ...