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 ...
随机推荐
- CCF计算机网络会议日期
SenSys: November 5-8 2017, Deadline: April 3, 2017 CoNEXT: December 12-15 2017, Deadline: June 12, 2 ...
- AngularJS 笔记1
2017-03-23 本文更新链接: http://www.cnblogs.com/daysme/p/6606805.html 什么是 angularjs 2009年有两个外国人创建,后由谷歌收购并开 ...
- HDU 1512 Monkey King(左偏树模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=1512 题意: 有n只猴子,每只猴子一开始有个力量值,并且互相不认识,现有每次有两只猴子要决斗,如果认识,就不打了 ...
- Spring-JDBC依赖
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</a ...
- JAVA 面向对象中的多态
多态是继封装.继承之后,面向对象的第三大特性. 现实事物经常会体现出多种形态,如学生,学生是人的一种,则一个具体的同学张三既是学生也是人,即出现两种形态. Java作为面向对象的语言,同样可以描述一个 ...
- Servlet模板,一个供新手参考的模板
由于这学期老师的进度是刚开始教JavaSE部分,而我的进度比较快,所以买了3本javaee的书,我根据自己的基础,选择了合适的开发实践,另外两本书都和框架相关,我自认为我的web基础还不是很牢固,所以 ...
- sklearn dataset 模块学习
sklearn.datasets官网:http://scikit-learn.org/stable/datasets/ sklearn.datasets 模块主要提供一些导入.在线下载及本地生成数据集 ...
- Spring的一个入门例子
例子参考于:Spring系列教材 以及<轻量级JavaEE企业应用实战> Axe.class package com.how2java.bean; public class Axe { p ...
- 剖析Hadoop和Spark的Shuffle过程差异
一.前言 对于基于MapReduce编程范式的分布式计算来说,本质上而言,就是在计算数据的交.并.差.聚合.排序等过程.而分布式计算分而治之的思想,让每个节点只计算部分数据,也就是只处理一个分片,那么 ...
- Java中类似C#中Task.wait()的类CountDownLatch
当主线程开辟多个子线程,而又需要这些子线程都执行完成后再执行主线程后续的操作,在C#中可以通过Task的wait方法来实现,然而在Java中也有类型的类CountDownLatch,具体用法如下: p ...