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. MIUI12系统怎么样开启Root超级权限的流程

    MIUI12系统能有啥方法开启root超级权限?各位都清楚,Android机器有root超级权限,如果手机开启root相关权限,可以实现更好的功能,举例子,各位公司的营销部门,使用某些营销软件都需要在 ...

  3. Linux网络基本网络配置方法介绍

    网络信息查看 设置网络地址: cat /etc/sysconfig/network-scripts/ifcfg-eth0 你将会看到: DEVICE=eth0BOOTPROTO=staticsHWAD ...

  4. lambda 怎么传递ref参数

    lambda 传递ref参数有个语法bug,必须要显式书写参数类型. //如 delegate bool FuncType(ref int num); FuncType func1; func1 = ...

  5. ES入门REST API

    在ES中存在4种数据对象,分别是 index  ,  type ,  document  , field .   其跟我们熟悉的关系型数据库得二维表得对应关系为: index -> table表 ...

  6. hbase 工作原理

    一.HBASE介绍HBase是一个高可靠性.高性能.面向列.可伸缩的分布式存储系统,利用HBase技术可在廉价PC Server上搭建大规模结构化的存储集群.HBase的目标是存储并处理大型数据,具体 ...

  7. Java设计模式视频讲解

    设计模式(JAVA) 视频网址: http://www.qghkt.com/ 设计模式(JAVA)视频地址: https://ke.qq.com/course/318643?tuin=a508ea62 ...

  8. IPV6地址中的%号什么意思

    在我配置ipv6 网络中,会发现ipv6地址后有%号,这表示什么呢? IPv6地址中的百分号是网卡interface标识.这个表示该地址仅限于标号为21的网络接口(一般指网卡或者虚拟网卡).而在其他网 ...

  9. Java基础系列--03_Java中的方法描述

    方法 (1)方法的定义:就是完成特定功能的代码块. 注意:在很多语言里面有函数的定义,而在Java中,函数被称为方法. (2)格式: 修饰符 返回值类型 方法名(参数类型 参数名1,参数类型 参数名2 ...

  10. 【题解】P2922 [USACO08DEC]秘密消息Secret Message

    \(\text{Tags}\) 字典树,统计 题意: 给出两组\(\text{0/1}\)串\(\text{A,B}\),求解\(\text{A}\)中某串是\(\text{B}\)中某串的前缀,和\ ...