Remove Element leetcode java
问题描述:
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的更多相关文章
- 169 Majority Element [LeetCode Java实现]
题目链接:majority-element /** * Given an array of size n, find the majority element. The majority elemen ...
- Remove Element leetcode
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- Leetcode练习题Remove Element
Leetcode练习题Remove Element Question: Given an array nums and a value val, remove all instances of tha ...
- [LeetCode] Remove Element 分析
Remove Element算是LeetCode的一道水题,不过这题也有多种做法,现就我所知的几种做一点讨论. 题目链接:https://leetcode.com/problems/remove-el ...
- [Leetcode][Python]27: Remove Element
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 27: Remove Elementhttps://oj.leetcode.c ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- LeetCode 027 Remove Element
题目要求:Remove Element Given an array and a value, remove all instances of that value in place and retu ...
- 【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 ...
- [LeetCode] Remove Element题解
Remove Element: Given an array and a value, remove all instances of that value in place and return t ...
随机推荐
- 剪格子|2013年蓝桥杯A组题解析第九题-fishers
剪格子 如图p1.jpg所示,3 x 3 的格子中填写了一些整数. 我们沿着图中的红色线剪开,得到两个部分,每个部分的数字和都是60. 本题的要求就是请你编程判定:对给定的m x n 的格子中的整数, ...
- ZOJ 3962 Seven Segment Display(数位DP)题解
题意:给一个16进制8位数,给定每个数字的贡献,问你贡献和. 思路:数位DP,想了很久用什么表示状态,看题解说用和就行,其他的都算是比较正常的数位DP. 代码: #include<iostrea ...
- 【做题】agc008f - Black Radius——计数&讨论&思维
原文链接 https://www.cnblogs.com/cly-none/p/9794411.html \[ \newcommand{\stif}[2]{\left[ \begin{matrix} ...
- 【20K必备知识点】北上广Java开发月薪20K往上,该如何做,需要会写什么
有人回答说这只能是大企业或者互联网企业工程师才能拿到.也许是的,小公司或者非互联网企业拿两万的不太可能是码农了,应该已经转管理.还有区域问题,这个不在我的考虑范围内,因为除了北上广深杭,其他地方也很难 ...
- shiro 前后端分离 seseeionId 问题
http://www.cnblogs.com/cshhs/p/9269411.html https://www.w3cschool.cn/shiro/rmvk1if1.html http://www. ...
- $mount方法是用来挂载我们的Vue.extend扩展的
html <body> <div id="app"> <diy></diy> </div> </body> ...
- [echarts] - echarts量化比较图表类型解析
https://echarts.baidu.com/examples/editor.html?c=watermark <!DOCTYPE html> <!--用作两种货品的参数对比- ...
- sublime配置 sublimecondeintel 分号后不要提示
https://github.com/SublimeCodeIntel/SublimeCodeIntel/issues/461 Thanks to @catgsmith ,I find a simil ...
- 51nod 1020 逆序排列
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1020 题意: 思路: 一开始用了三重循环... 设f(n,k)表示n个数 ...
- python学习 day09打卡 初识函数
本节内容: 1.什么是函数 2.函数定义,函数名,函数体及函数的调用 3.函数的返回值 4.函数的参数 一.什么是函数 函数:对代码块和功能的封装和定义 定义一个事情或者功能.等到需要的时候直接去用, ...