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 in place with constant memory.

The order of elements can be changed. It doesn't matter what you leave beyond the new length.

Example:

Given input array nums = [3,2,2,3]val = 3

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

解法:

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

public class Solution {
public int removeElement(int[] nums, int val) {
if (nums == null || nums.length == 0) {
return 0;
} int newLength = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] != val) {
nums[newLength++] = nums[i];
}
}
return newLength;
}
}

[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

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

  9. 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 ...

  10. [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. Weighted Median

    For n elements x1, x2, ..., xn with positive integer weights w1, w2, ..., wn. The weighted median is ...

  2. 基于spec评论作品 - 探路者 贪吃蛇

    基于spec评论作品,试用(并截图)所有其他小组的Alpha作品,与软件功能说明书对比,评论Alpha作品对软件功能说明书的实现. 首先通过命令行进入到游戏主页面中. 因为软件没有编译为exe程序,所 ...

  3. 评价Win8自带输入法

    对于人机交互设计,有以下四个基本原则:从用户角度考虑.从头到尾记住用户选择.短期刺激和长期使用的好处坏处.不让用户犯简单错误.我用的最多的是我的系统自带的输入法,评价的也只能是它了. 1.从用户角度: ...

  4. 团队作业4——第一次项目冲刺(Alpha版本)第一次

    一.会议内容 制定任务内容 制作leangoo表格 初步工作 二.各人工作 成员 计划任务 遇见难题 贡献比 塗家瑜(组长) 后端与数据库通讯 无 1 张新磊 表设计 无 1 姚燕彬 测试计划编写 无 ...

  5. 使用fprof基本步骤

    $erl -name a@localhost -setcookie abc -remsh b@localhost >fprof:trace([start, {file, "/home/ ...

  6. beta阶段——项目复审

    beta阶段--项目复审 小组的名字和链接 优点 缺点 bug 排名顺序 颜罗王team http://www.cnblogs.com/LDLYMteam 界面清新,音乐能够选择是否播放,词汇按照四六 ...

  7. php自带的filter过滤函数

    PHP 过滤器用于对来自非安全来源的数据(比如用户输入)进行验证和过滤. filter_has_var()检查是否存在指定输入类型的变量. filter_id()返回指定过滤器的 ID 号. filt ...

  8. shit Rap & mock api

    shit Rap & mock api https://thx.github.io/RAP/study.html https://github.com/thx/RAP/wiki/quick_g ...

  9. Linux 重定向输出到多个文件中

    转自:http://codingstandards.iteye.com/blog/833695 用途说明 在执行Linux命令时,我们可以把输出重定向到文件中,比如 ls >a.txt,这时我们 ...

  10. SpingCloud之feign框架调用

    1.生产者(没有什么特殊性) pom.xml <?xml version="1.0" encoding="UTF-8"?> <project ...