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]);
}

这道题让我们移除一个数组中和给定值相同的数字,并返回新的数组的长度。是一道比较容易的题,只需要一个变量用来计数,然后遍历原数组,如果当前的值和给定值不同,就把当前值覆盖计数变量的位置,并将计数变量加1。代码如下:

class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int res = ;
for (int i = ; i < nums.size(); ++i) {
if (nums[i] != val) nums[res++] = nums[i];
}
return res;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/27

类似题目:

Remove Duplicates from Sorted Array

Remove Linked List Elements

Move Zeroes

参考资料:

https://leetcode.com/problems/remove-element/

https://leetcode.com/problems/remove-element/discuss/12286/Accepted-java-solution

https://leetcode.com/problems/remove-element/discuss/12289/My-solution-for-your-reference.

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 27. Remove Element 移除元素的更多相关文章

  1. [LeetCode]27. Remove Element移除元素

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

  2. 27. Remove Element - 移除元素-Easy

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

  3. leetCode 27.Remove Element (删除元素) 解题思路和方法

    Remove Element Given an array and a value, remove all instances of that value in place and return th ...

  4. LeetCode 27. Remove Element (移除元素)

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

  5. LeetCode 27 Remove Element (移除数组中指定元素)

    题目链接: https://leetcode.com/problems/remove-element/?tab=Description   Problem : 移除数组中给定target的元素,返回剩 ...

  6. [LeetCode] Remove Element 移除元素

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

  7. [leetcode]27. Remove Element删除元素

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

  8. 027 Remove Element 移除元素

    给定一个数组和一个值,在这个数组中原地移除指定值和返回移除后新的数组长度.不要为其他数组分配额外空间,你必须使用 O(1) 的额外内存原地修改这个输入数组.元素的顺序可以改变.超过返回的新的数组长度以 ...

  9. Leetcode 27——Remove Element

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

随机推荐

  1. jQuery 源码分析(二) 入口模块

    jQuery返回的对象本质上是一个JavaScript对象,而入口模块则可以保存对应的节点的引用,然后供其它模块操作 我们创建jQuery对象时可以给jQuery传递各种不同的选择器,如下: fals ...

  2. CodeForce 222C Reducing Fractions

    To confuse the opponents, the Galactic Empire represents fractions in an unusual format. The fractio ...

  3. poj-2234 Matches Game Nim

    Matches Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13264   Accepted: 7712 Des ...

  4. Oracle 11g 服务端的安装步骤

    Ø  简介 本文主要介绍 Oracle 11g 服务端的安装步骤,在介绍之前说明以下几点: 1.   所安装的服务器是本机的虚拟机,操作系统为 Windows Server 2019: 2.   以下 ...

  5. 动画展现十大经典排序算法(附Java代码)

    0.算法概述 0.1 算法分类 十种常见排序算法可以分为两大类: 比较类排序:通过比较来决定元素间的相对次序,由于其时间复杂度不能突破O(nlogn),因此也称为非线性时间比较类排序. 非比较类排序: ...

  6. PHP7 php_memcache.dll下载

    因为项目切换到PHP7.1的环境,而且要用到memcache,但是在pecl上却发现memcache不支持PHP7.在网上也找了很久也没有找到,基本上都是大牛自己编译的dll,和自己的版本不合适. 结 ...

  7. Ubuntu Err:1 http://us.archive.ubuntu.com/ubuntu bionic InRelease Could not resolve 'us.archive.ubuntu.com' 错误

    Ubuntu 更新 apt-get update 的时候 出现 Err: http://us.archive.ubuntu.com/ubuntu bionic InRelease Could not ...

  8. 一张图搞定 .NET Framework, .NET Core 和 .NET Standard 的区别

    最近开始研究.NET Core,有张图一看就能明白他们之前的关系. 上图己经能够说明.NET Framework和.NET Core其实是实现了 .NET Standard相关的东西,或者说Frame ...

  9. C# winform 获取鼠标点击位置

    说明:该篇随笔的代码内容并非出自本人,是在其他网站搜寻的,出处已经不记得了,本次随笔只为记录,目的帮助自己,帮助他人. 实现的原理也不做多的赘述,直接上代码. 第一个类是需要用到的Windows AP ...

  10. WinForms中动态给treeView的节点添加ContextMenuStrip,并绑定Click事件

    生成ContextMenuStrip var docMenu = new ContextMenuStrip(); ToolStripMenuItem deleteMenuItem = new Tool ...