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 ...
随机推荐
- 为什么不允许使用 Java 静态构造函数?
不允许使用 Java 静态构造函数,但是为什么呢?在深入探讨不允许使用静态构造函数的原因之前,让我们看看如果要使 构造函数静态化 会发生什么. Java 静态构造函数 假设我们有一个定义为的类: pu ...
- selenium常用的三种等待方式
一.强制等待 使用方法:sleep(X),等待X秒后,进行下一步操作. 第一种也是使用最简单的一种办法就是强制等待sleep(X),强制让浏览器等待X秒,不管当前操作是否完成,是否可以进行下一步操作, ...
- Fusionstorage的逻辑架构
Fusionstorage Fusionstorage的逻辑架构 Mdc:元数据控制,实现对分布式集群的状态控制,以及控制数据分布式规则,数据重建规则等,mdc默认部署在3个节点的zk盘上,形成mdc ...
- 【linux】切换到root用户,并重置root用户密码
1.切换当前用户 到 root用户 sudo -i 2.重置root用户密码 sudo passwd root
- python爬虫:将数据保存到本地
一.python语句存储 1.with open()语句 with open(name,mode,encoding) as file: file.write() name:包含文件名称的字符串; mo ...
- MySQL EXPLAIN 语句
对于 MySQL 在执行时来说,EXPLAIN 功能上与 DESCRIBE 一样.实际运用中,后者多用来获取表的信息,而前者多用于展示 MySQL 会如何执行 SQL 语句(Obtaining Exe ...
- PHP面试题2019年新浪工程师面试题及答案解析
一.单选题(共28题,每题5分) 1.以下语句输出的结果是什么? A.3$a\$a3336 B.33\$a3336 C.$a$a\$a3336 D.3$a\$a333$a$a 参考答案:A 答案解析: ...
- ABP进阶教程11 - 小结
点这里进入ABP进阶教程目录 效果预览 至此,ABP进阶教程的查询/分页/排序/导出/打印示例已完成,效果如下 登录 首页 办公室信息 院系信息 课程信息 教职员信息 学生信息 新增 修改 删除 查询 ...
- SpringBoot+Thyemelaf开发环境正常,打包jar发到服务器就报错Template might not exist or might not be accessible
这里说一下Thyemelaf的巨坑 写了一个SpringBoot+Thyemelaf的项目,并不是前后端分离.今天想放到linux服务器上玩玩,打成jar包,然后一运行他妈居然报错了,报了一个Temp ...
- [20191002]函数dump的bug.txt
[20191002]函数dump的bug.txt --//前几天写raw转化oracle number脚本,在使用函数dump时遇到一些问题,做一个记录:--//oracle number 0 编码 ...