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. layui table 中固定列的行高和table行高不一致

    解决方法:只需在done回调函数执行以下方法 done: function(res, curr, count){ $(".layui-table-main tr").each(fu ...

  2. shell变量及相关命令

  3. codelite配置信息

    codelite下编译执行wxwidgets库需要修改链接库如下: 原来的c++ compiler配置-g;-O0;-Wall;$(shell wx-config --cflags --debug) ...

  4. 力扣—Remove Duplicates from Sorted List(删除排序链表中的重复元素)python实现

    题目描述: 中文: 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2输出: 1->2 示例 2: 输入: 1->1->2 ...

  5. js插件-简单拖拽

    前端开发的时候,有好多地方用到拖拽效果,当然 http://jqueryui.com/draggable/  是个不错的选择,but 我是个打破砂锅问到底的人,抽点时间用js小小的实现了类似的插件,话 ...

  6. CDN技术之--全局负载均衡(GSLB)

    负载均衡就是智能调度全局负载均衡(GSLB)的负载均衡主要是在多个节点之间进行均衡,其结果可能直接终结负载均衡过程,也可能将用户访问交付下一层次的(区域或本地)负载均衡系统进行处理.GSLB最通用的是 ...

  7. shell(计算机壳层)(一)

    在计算机科学中,Shell俗称壳(用来区别于核),是指“提供使用者使用界面”的软件(命令解析器).它类似于DOS下的command和后来的cmd.exe.它接收用户命令,然后调用相应的应用程序. wi ...

  8. 【Java架构:基础技术】一篇文章搞掂:Maven

    本文篇幅较长,建议合理利用右上角目录进行查看(如果没有目录请刷新). 本文基于<Maven 实战>一书进行总结和扩展,大家也可以自行研读此书. 一.Maven简介 1.1.什么是Maven ...

  9. Android获取手机和系统版本等信息的代码

    有时候需要统计手机的型号和版本号,利用程序可以获取到相应的手机信息,对比两部手机发现,厂商不同,某个信息显示方式也不尽相同,具体见: String phoneInfo = "Product: ...

  10. scrapy原理

    scarpy据说是目前最强大的爬虫框架,没有之一.就是这么自信. 官网都是这么说的. An open source and collaborative framework for extracting ...