问题描述:

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

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

问题分析:给定一个数组,一个value值,从这个数组中删除所有值为value的元素,并返回数组length

算法:

方法一:借助另外一个list,耗费空间

public static int removeElement(int nums[],int val){

        List<Integer> list = new ArrayList<Integer>(); //将数据暂时存放在list中
for (int i = 0; i < nums.length; i++) {
if(nums[i] != val)
list.add(nums[i]);
} if(list.size() != 0){
for (int i = 0; i < list.size(); i++) { //再将list中的数据写回数组中
nums[i] = list.get(i);
}
} return list.size() ; //返回数组length
}

方法二:采用两个指针,不需要额外空间,数组原地做修改

public int removeElement(int[] nums, int val) {
//原地修改,不需要额外的空间
int newindex = 0;
for (int i = 0; i < nums.length; i++) {
if(nums[i] != val)
nums[newindex++] = nums[i];
} return newindex;
}

Remove Element leetcode java的更多相关文章

  1. 169 Majority Element [LeetCode Java实现]

    题目链接:majority-element /** * Given an array of size n, find the majority element. The majority elemen ...

  2. Remove Element leetcode

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

  3. Leetcode练习题Remove Element

    Leetcode练习题Remove Element Question: Given an array nums and a value val, remove all instances of tha ...

  4. [LeetCode] Remove Element 分析

    Remove Element算是LeetCode的一道水题,不过这题也有多种做法,现就我所知的几种做一点讨论. 题目链接:https://leetcode.com/problems/remove-el ...

  5. [Leetcode][Python]27: Remove Element

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 27: Remove Elementhttps://oj.leetcode.c ...

  6. 27. Remove Element【leetcode】

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

  7. LeetCode 027 Remove Element

    题目要求:Remove Element Given an array and a value, remove all instances of that value in place and retu ...

  8. 【LeetCode】27. Remove Element (2 solutions)

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

  9. [LeetCode] Remove Element题解

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

随机推荐

  1. JVM启动参数大全

    java启动参数共分为三类: 其一是标准参数(-),所有的JVM实现都必须实现这些参数的功能,而且向后兼容: 其二是非标准参数(-X),默认jvm实现这些参数的功能,但是并不保证所有jvm实现都满足, ...

  2. P5159 WD与矩阵

    思路 奇怪的结论题 考虑增量构造,题目要求每行每列都有偶数个1,奇偶性只需要增减1就能够调整了,所以最后一列一行一定能调整前面n-1阶矩阵的值,所以前面可以任选 答案是\(2^{(n-1)(m-1)} ...

  3. Termux 一款安卓终端模拟器

    Termux官网 https://termux.com/ 用处 提供了一个类似于Linux终端的界面,可以使用apt安装程序.目前我在上面跑了我以前写的一些爬虫脚本,运行完全没有问题. 玩法 玩法还是 ...

  4. Lintcode241-String to Integer - Naive

    Given a string, convert it to an integer. You may assume the string is a valid integer number that c ...

  5. URL简单梳理

    # DEBUG模式: 开启debug模式后,修改项目代码时按下ctrl+s可重启项目: 项目中出现bug时,浏览器与控制台会打印错误信息: 在生产环境中禁止开启DEBUG模式,有很大的安全隐患: 将D ...

  6. Centos7下安装memcached

    1. which memcached //如果已经安装,会有“/usr/bin/memcached”类似的输出 memcached -h //memcache帮助列表 php -m | grep me ...

  7. phantomjs 下载

    http://phantomjs.org/download.html

  8. C++.可变参数_ZC测试

    ZC:环境: Win7 x64(旗舰版),Microsoft Visual Studio 2010(版本 10.0.30319.1 RTMRel, Microsoft .NET Framework(版 ...

  9. java——File

    注意事项: 1:创建File对象需要导包, import java.io.File 2:File对象没有无参数构造.创建对象需要传参. 3:File类的对象,既可以代表文件也可以代表文件夹.   构造 ...

  10. nginx 启动报错 1113: No mapping for the Unicode character exists in the target multi-byte code

    failed (1113: No mapping for the Unicode character exists in the target multi-byte code page) 因为路径有中 ...