Leetcode练习题Remove Element
Leetcode练习题Remove Element
Question:
Given an array nums and a value val, 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 by modifying the input array in-place with O(1) extra memory.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
Example 1:
Given nums = [3,2,2,3], val = 3,
Your function should return length = 2, with the first two elements of nums being 2.
It doesn't matter what you leave beyond the returned length.
Example 2:
Given nums = [0,1,2,2,3,0,4,2], val = 2,
Your function should return length = 5, with the first five elements of nums containing 0, 1, 3, 0, and 4.
Note that the order of those five elements can be arbitrary.
It doesn't matter what values are set beyond the returned length.
Solution:
class Solution {
public int removeElement(int[] nums, int val) {
int newIndice = 0;
for(int i=0;i<nums.length;i++)
{
if(nums[i]!=val)
{
nums[newIndice++] = nums[i];
}
}
return newIndice;
}
}
在本题中,主要考点是利用两个指针来实现数组内的内容判断和复制。比如,在本题中我利用newIndice来作为一个比原始指针慢的指针来指向过滤后内容的值,并自增,就实现了目标,得到了新的数组。
另外有趣的是,我将本题看成计算数组中有多少个不重复数组的值;另外延伸一下,再计算每个不重复值的个数;
接下来我们一次考虑:
- 计算数组中有多少个不重复的值:
思路是使用Hashset来计算,因为HashSet中,每个值仅允许出现一次,所以当我们将值都add进去的时候。
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
public class hashSet {
public static void main(String[] args)
{
int[] arrays = {1,4,5,2,6,1,10,3};
hashset(arrays);
}
public static void hashset(int[] arrays)
{
HashSet hashset = new HashSet();
for(Integer a: arrays)
{
hashset.add(a);
}
Iterator iterator = hashset.iterator();
while (iterator.hasNext())
{
System.out.println(iterator.next());
}
}
}
Leetcode练习题Remove Element的更多相关文章
- leetCode 27.Remove Element (删除元素) 解题思路和方法
Remove Element Given an array and a value, remove all instances of that value in place and return th ...
- 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 (移除元素)
Given an array and a value, remove all instances of that value in place and return the new length. D ...
- [LeetCode] 27. Remove Element 移除元素
Given an array nums and a value val, remove all instances of that value in-place and return the new ...
- LeetCode 27 Remove Element
Problem: Given an array and a value, remove all instances of that value in place and return the new ...
- 【leetcode】Remove Element
题目概述: Given an array and a value, remove all instances of that value in place and return the new len ...
- 【leetcode】Remove Element (easy)
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- Leetcode 27 Remove Element STL
和remove zero类似的方法完成该题 class Solution { public: int removeElement(vector<int>& nums, int va ...
- Java for LeetCode 027 Remove Element
Given an array and a value, remove all instances of that value in place and return the new length. T ...
随机推荐
- 详解JAVA8函数式接口{全}
1: 函数式接口 1.1 概念 1.2 格式 1.3@FunctionalInterface注解 1.4 调用自定义函数接口 2:函数式编程 2.1:lambda的延迟执行 2.2 使用Lambda作 ...
- hyper-v Centos7 网卡配置无效
环境: Win 10 Hyper-v 安装虚拟机:Centos 7 遇到问题: 网络配置无效,使用命令“ip addr” 网卡没有出现在列表中,显示了一个奇怪的网卡名字如“enp0s010f”,配置文 ...
- python3在mac下配置
目的 https://github.com/VonSdite/Plane_Wars 可以本地跑起来. 下载并安装python3 https://www.python.org/downloads/mac ...
- TCP协议 - 面向连接
一.TCP特性概览 1.面向连接 TCP是基于连接进行数据交互,通信双方在进行数据交互之前需要建立连接,该连接也只能用在双方之间进行交互.这点不像UDP中的组播和广播,可以在同一组中多个主机交互数据. ...
- ABP开发框架前后端开发系列---(6)ABP基础接口处理和省份城市行政区管理模块的开发
最近没有更新ABP框架的相关文章,一直在研究和封装相关的接口,总算告一段落,开始继续整理下开发心得.上次我在随笔<ABP开发框架前后端开发系列---(5)Web API调用类在Winform项目 ...
- .net core入门-跨域访问配置
Asp.net Core 跨域配置 一般情况WebApi都是跨域请求,没有设置跨域一般会报以下错误 No 'Access-Control-Allow-Origin' header is prese ...
- python基础(8):基本数据类型三(dict)、is和==、编码和解码
1. 字典 1.1 字典的介绍 字典(dict)是python中唯一的一个映射数据类型,它是以{}括起来的键值对组成.在dict中key是唯一的,在保存的时候,根据key来计算出一个内存地址,然后将k ...
- Python爬虫使用selenium爬取qq群的成员信息(全自动实现自动登陆)
前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: python小爬虫 PS:如有需要Python学习资料的小伙伴可以 ...
- SpringBoot(十一):SpringBoot整合Redis
详解springboot整合redis:https://blog.csdn.net/qq_36781505/article/details/86612988 一.环境准备 Redis-x64-3.2. ...
- jQuery之概念以及基本使用
1. jQuery的概述 1.1 jQuery的概念 jQuery是一个快速.简洁的JavaScript库,其设计的宗旨是“Write Less,Do More” jQuery主要是封装了JavaSc ...