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 = [,,,], val = ,

Your function should return length = , with the first two elements of nums being .

It doesn't matter what you leave beyond the returned length.

Example 2:

Given nums = [,,,,,,,], val = ,

Your function should return length = , with the first five elements of nums containing , , , , and .

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 = ; i < len; i++) {
print(nums[i]);
}

我的解题思路:遍历整个数组,逐个检查每个值是否与要求移除的值相同,如果相同,则将后面的值依次向前移动一个位置,并从当前位置重新开始检查。

遍历结束之后再检查最后一个位置的数是不是要求移除的值,如果是 返回值减1;

看代码:

public class Solution {
public int RemoveElement(int[] nums, int val) {
if(nums.Length == )
return ;
int i,j,length=nums.Length;
for(i = ; i < length; i++){
if(nums[i] == val){
for(j = i; j < length - ; j++){
nums[j] = nums[j+];
}
length--;
i--;//i递减1之后,从当前位置重新检查
}
}
if(length != && nums[length-] == val)//检查最后一个数字是不是需要移除的值
length--;
return length;
}
}

LeetCode Array Easy 27. Remove Element 解题的更多相关文章

  1. leetCode练题——27. Remove Element

    1.题目 27. Remove Element——Easy Given an array nums and a value val, remove all instances of that valu ...

  2. C# 写 LeetCode easy #27 Remove Element

    27. Remove Element Given an array nums and a value val, remove all instances of that value in-place  ...

  3. 【LeetCode】27. Remove Element 解题报告(Python & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 记录起始位置 日期 题目地址:https:/ ...

  4. LeetCode Array Easy 26.Remove Duplicates from Sorted Array 解答及疑惑

    Description Given a sorted array nums, remove the duplicates in-place such that each element appear ...

  5. (Array)27. Remove Element

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

  6. [LeetCode&Python] Problem 27. Remove Element

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

  7. LeetCode记录之27——Remove Element

    这道题跟26题很类似,并且有官方的答案.看了官方的答案之后发现写得特别巧,自己做的题太少思路太窄.有意思的是我的算法的时间复杂度是O(N^2),官方的是O(N),我的实际运行时间还少了2ms. ive ...

  8. 【leetcode❤python】27. Remove Element

    #-*- coding: UTF-8 -*- class Solution(object):    def removeElement(self, nums, val):        "& ...

  9. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

随机推荐

  1. mysql常见的hint

    mysql常用的hint对于经常使用oracle的朋友可能知道,oracle的hint功能种类很多,对于优化sql语句提供了很多方法.同样,在mysql里,也有类似的hint功能.下面介绍一些常用的. ...

  2. Blueprint的实现

    Blueprint其实本身只是对view上的接口进行了注册,然后整体挂载在app上,Blueprint本身的目的就是组织多模块的平行共存,避免直接在app上注册view,其实更多的只是方便开发和代码的 ...

  3. C++ I/O库练习

    编写函数,以读模式打开一个文件,将其内容读入到一个string的vector中,将每一行作为一个独立的元素存于vector中,并输出. 思路:1.以读的模式打开文件“目录.txt”: 2.先创建str ...

  4. jQuery HTML-设置

    例子1 html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> &l ...

  5. java中形参中的 “. . .” 是什么意思

    如这个jdbc中封装的绑定参数的方法: /** * 绑定参数 * @param pstmt * @param os */ public static void executebindParam(Pre ...

  6. CSP-S2019退役记。。。

    模拟赛的时候题目就比较迷,感觉不像联赛难度的. 考完正式赛才觉得这TM算个P. Day1: 写密码的监考同学的蜜汁字体让我傻了. 0和O是一样的,9和q是一样的,1和l是一样的-- 又没有冷静下来发现 ...

  7. python--闭包函数、装饰器

    先来点补充. x= def foo(): print(x) x= foo() 结果: x= def foo(): global x x= print(x) foo() print(x) 结果: x= ...

  8. 硬币问题 (dp,多重背包的二分优化)

    题目描述 给你n种硬币,知道每种的面值Ai和每种的数量Ci.问能凑出多少种不大于m的面值. 输入 有多组数据,每一组第一行有两个整数 n(1≤n≤100)和m(m≤100000),第二行有2n个整数, ...

  9. nginx的基础概念

    http://tengine.taobao.org/book/index.html   算是看书笔记吧,太多了就用自己的话写一下了 nginx是以多进程 的方式来工作的,启动时会有一个master进程 ...

  10. Security基础(一):Linux基本防护措施、使用sudo分配管理权限、提高SSH服务安全

    一.Linux基本防护措施 目标: 本案例要求练习Linux系统的基本防护措施,完成以下任务: 修改用户zhangsan的账号属性,设置为2015-12-31日失效(禁止登录) 锁定用户lisi的账户 ...