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.

分析:in-place就地remove元素,不允许分配另一个数组。可以这么想,允许用另一个数组的话,直接遍历这个数组,满足条件就添加到另一个数组中。但是不让用多一个数组的话,就用自身,因为满足条件的数量肯定是小于等于数组的长度,所以用一个index来计数即可,满足条件的放到index位置上,index++。
class Solution {
public int removeElement(int[] nums, int val) {
int index=0;
for(int i=0;i<nums.length;i++){
if(nums[i]!=val){
nums[index]=nums[i];
index++;
}
}
return index; } }

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

  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. C# 通过smtp服务器进行邮件发送 MailHelper

    C# 通过smtp服务器进行邮件发送 MailHelper.cs using System; using System.Data; using System.Configuration; using ...

  2. Asp.net的sessionState四种模式配置方案

    sessionState节点的配置 web.config关于sessionState节点的配置方案,sessionState有四种模式:off,inProc,StateServer,SqlServer ...

  3. 加深try catch Finnly的理解

    上代码 public String twoGetFeeInfoByWithUnit(JSONArray jsonArray,String key1,String key2){ String Debit ...

  4. 异常-----freemarker.core.ParseException: Encountered "string"

    1.错误描述 freemarker.core.ParseException: Encountered "string" at line 21, column 21 in type. ...

  5. ubuntu安装pycharm桌面快捷方式

    1.sudo gedit /usr/share/applications/Pycharm.desktop 2.输入 [Desktop Entry] Encoding=UTF-8 Name=Pychar ...

  6. 使用Aspose将DataTable转Excel

    0.准备工作   1.下载并引入Aspose.Cells 下载Aspose Cells并引入using Aspose.Cells 下面示例中用的是.net 3.0版本的Aspose Cells,编译环 ...

  7. 【BZOJ1717】产奶的模式(后缀数组)

    [BZOJ1717]产奶的模式(后缀数组) 题面 权限题 hihocoder 洛谷 题解 \(hihocoder\)里面讲的非常好了 这题要求的就是最长可重叠重复K次子串 所谓相同的子串 我们可以理解 ...

  8. 【NOIP2012】疫情控制(二分,倍增,贪心)

    洛谷上的题目链接,题目不在赘述 题解 既然要时间最短,首先考虑二分. 因此,考虑二分时间,问题转换为如何检查能否到达. 如果一支军队一直向上走,能够到达根节点,那么他可以通过根节点到达其他的节点,因此 ...

  9. ssr 服务端安装教程

    1 ShadowsocksR 多用户版服务端安装教程(SS-Panel后端) 2 ShadowsocksR 单用户版服务端安装教程

  10. 17.HTML

    HTML简介 htyper text markup language  即超文本标记语言. 超文本: 就是指页面内可以包含图片.链接,甚至音乐.程序等非文字元素. 标准模板 <!DOCTYPE ...