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 of nums containing 0, 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的更多相关文章

  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 (移除元素)

    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 nums and a value val, remove all instances of that value in-place and return the new ...

  4. LeetCode 27 Remove Element

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

  5. Leetcode 27 Remove Element STL

    和remove zero类似的方法完成该题 class Solution { public: int removeElement(vector<int>& nums, int va ...

  6. Java [leetcode 27]Remove Element

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

  7. Leetcode 27——Remove Element

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

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

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

随机推荐

  1. 生鲜配送管理系统_升鲜宝 V2.0 小程序辅助系统工具矩阵系列相关说明

    随着微信红利的进一步释放,使用人群的不断增加,小程序从2017年01月第一批开发者出现后,2018年小程序得到快速的提升,小程序开发的相关应用小工具得到了市场的青咪,社会化大分工.协同.共享.协作的思 ...

  2. 可达用户投资额的计算(Java)

    有话要说: 前阵子遇到了一个计算可达用户投资额的问题,觉得非常有趣,故把它记录下来. 问题描述: 某产品可被投资,注册后才可以投资,其注册可以被邀请(并不是每个人都是被邀请的).邀请人可以邀请多个人注 ...

  3. go get获取gitlab私有仓库的代码

    目录 目录 1.Gitlab的搭建 2.如何通过go get,获取Gitlab的代码 目录 1.Gitlab的搭建   在上一篇文章中,已经介绍了如何搭建Gitlab Https服务<Nginx ...

  4. 如何将Eclipse的javaWeb项目改为IDEA的maven项目

    1.首先去IDEA开发工具创建一个maven项目,把该项目改为Web项目, a.在pom.xml中,添加packaging标签,值为war b.右键File,选中project structure, ...

  5. Docker-单宿主机下的网络模式

    docker利用namespaces和cgroups实现了应用隔离和资源控制,那么网络层优势如何实现的呢?是直接使用宿主机的网卡设备,还是独立创造出自己的网络设备?以及容器如何与外界通信,下面我们通过 ...

  6. Go 反射

    基本了解 在Go语言中,大多数时候值/类型/函数非常直接,要的话,定义一个.你想要个Struct type Foo struct { A int B string } 你想要一个值,你定义出来 var ...

  7. 基于IPv6的数据包分析(第三组)

    一.实验拓扑 二.配置过程 本处提供R1.R2.R4的详细配置过程(包含静态路由的配置) 1)      R1: R1(config)#int e1/0 R1(config-if)#ipv6 addr ...

  8. pyhton崩溃的第六天,又有新成员

    首先在今天的刚刚开始,补充一下上次两个成员的一些特有的方法,一个是列表,一个是字典,首先列表里面多了一个反转的方法,名叫reverse,简单就是把列表中的123变成了321,还有一个方法是sort,是 ...

  9. 在Linux系统安装Nodejs 最简单步骤

    1.去官网下载和自己系统匹配的文件: 英文网址:https://nodejs.org/en/download/ 中文网址:http://nodejs.cn/download/ 通过  uname -a ...

  10. Linux:Day20(上) openssh和CA

    ssh:secure shell protocol,22/tcp,安全的远程登陆 OpenSSH:ssh协议的开源实现: dripbear:另一个开源实现: SSH协议版本 v1:基于CRC-32做M ...