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 ...
随机推荐
- 转载-ThreadPoolExecutor里面4种拒绝策略(详细)
原文链接:https://blog.csdn.net/wjs19930820/article/details/79849050 1 /** * 定义异步任务执行线程池 */ @Configuratio ...
- MySQL UNSIGNED和ZEROFILL属性
UNSIGNED 这个属性就是标记数字类型是无符号的,和C/C++语言中的unsigned含义是一样的,int signed的类型范围是-2147483648~2147483648,而int unsi ...
- VS2019 MSB364 未找到框架“NETFramework,Version=v4.7”
环境: WIN 10 VS2019 已安装框架v4.7.2 问题: 在打开一些早期项目时,编译报 MSB364 错误,未找到框架“NETFramework,Version=v4.7”或未找到框架“NE ...
- C++ 类的static静态成员
静态static 静态成员的提出是为了解决数据共享的问题.实现共享有许多方法,如:设置全局性的变量或对象是一种方法.但是,全局变量或对象是有局限性的. 在全局变量前,加上关键字static该变量就被定 ...
- Java 生态圈知识汇总
原文地址:github.com/aalansehaiy… 前言 有人认为编程是一门技术活,要有一定的天赋,非天资聪慧者不能及也.其实不然,笔者虽是计算机专业出身,但工作年限并不长,对于技术这碗饭有一些 ...
- Ubuntu安装CUDA、CUDNN比较有用的网址总结
Ubuntu安装CUDA.CUDNN比较有用的网址总结 1.tensorflow各个版本所对应的的系统要求和CUDA\CUDNN适配版本 https://tensorflow.google.cn/in ...
- python执行shell实时输出
1.使用readline可以实现 import subprocess def run_shell(shell): cmd = subprocess.Popen(shell, stdin=subproc ...
- Java生鲜电商平台-App系统架构开发与设计
Java生鲜电商平台-App系统架构开发与设计 说明:阅读此文,你可以学习到以下的技术分享 1.Java生鲜电商平台-App架构设计经验谈:接口的设计2.Java生鲜电商平台-App架构设计经验谈:技 ...
- 关于spring,IOC和AOP的解析原理和举例
引用自:https://blog.csdn.net/paincupid/article/details/43152397 IOC:就是DAO接口的实现不再是业务逻辑层调用工厂类去获取,而是通过容器(比 ...
- PHP mysqli_rollback MySQLi 函数
定义和用法 mysqli_rollback - 回退当前事务 语法: mysqli_rollback ( mysqli $link ) 参数 参数 必需的 描述 link 是 由mysqli_conn ...