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 ...
随机推荐
- Google Protobuf结合Netty实践
1.Win版Protobuf代码生成工具下载: https://github.com/protocolbuffers/protobuf/releases 注意下载protoc-3.6.1-win32. ...
- SpringCloud与Consul集成实现负载均衡
一.背景 SpringCloud微服务目前比较流行,其中大都在使用的服务注册与发现是Eureka,最近研究了Consul的集群搭建,现使用Consul实现服务的负载均衡.其主要拓扑结构如下: 二.Co ...
- 【原理、应用】Quartz集群原理及配置应用
一.Quartz任务调度的基本实现原理 Quartz是OpenSymphony开源组织在任务调度领域的一个开源项目,完全基于Java实现.作为一个优秀的开源调度框架,Quartz具有以下特点: 强大的 ...
- P3980 [NOI2008]志愿者招募
思路 巧妙的建图 因为每个志愿者有工作的时段,所以考虑让一个志愿者的流量能够从S流到T产生贡献 所以每个i向i+1连INF-a[x]的边(类似于k可重区间集),每个si向ti连边cap=INF,cos ...
- The more... the more句型
百度文库:https://wenku.baidu.com/view/a7f1067f59fb770bf78a6529647d27284a73374b.html the more ... , the m ...
- (转载)Sublime Text 3 快捷键大全
选择类Ctrl+D 选中光标所占的文本,继续操作则会选中下一个相同的文本.Alt+F3 选中文本按下快捷键,即可一次性选择全部的相同文本进行同时编辑.举个栗子:快速选中并更改所有相同的变量名.函数名等 ...
- 第五个神奇的电梯(代码抢先看<1>)
关于一些自认为比较独特的设计思路,也不知道是好还是坏,放在这里让大家一起看一下. 关于mian函数:因为采用了注册机制所以主函数比较简单. #include "stdafx.h" ...
- HDU 3400 Line belt (三分套三分)
http://acm.split.hdu.edu.cn/showproblem.php?pid=3400 题意: 有两条带子ab和cd,在ab上的速度为p,在cd上的速度为q,在其它地方的速度为r.现 ...
- React Native原生模块向JS传递数据的几种方式(Android)
一般情况可以分为三种方式: 1. 通过回调函数Callbacks的方式 2. 通过Promises的异步的方式 3. 通过发送事件的事件监听的方式. 参考文档:传送门
- React native中的组建通知通信:
有这么一个需求,在B页面pop()回到A页面,需要A页面执行刷新,那么我们可以采用以下方法: 1:在A页面Push到B页面中,加上一个A页面中的刷新函数做为参数,然后在B页面中在pop()函数封装后通 ...