LeetCode & Q26-Remove Duplicates from Sorted Array-Easy
Descriptions:
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.For example,
Given input array nums =
[1,1,2],Your function should return length =
2, with the first two elements of nums being1and2respectively. It doesn't matter what you leave beyond the new length.
我写的一直有问题...用了HashSet集合,没有研究过这个类型,[1,1,2]输出结果一直是[1,1]
(问题一发现,在于,题目要求的是改变nums[]的内容,而不是输出一个新的数组)
(在小本本上记下,要研究HashSet)
import java.util.HashSet;
import java.util.Set;
public class Solution {
public static int removeDuplicates(int[] nums) {
Set<Integer> tempSet = new HashSet<>();
for(int i = 0; i < nums.length; i++) {
Integer wrap = Integer.valueOf(nums[i]);
tempSet.add(wrap);
}
return tempSet.size();
}
}
下面是优秀答案
Solutions:
public class Solution {
public static int removeDuplicates(int[] nums) {
int j = 0;
for(int i = 0; i < nums.length; i++) {
if(nums[i] != nums[j]) {
nums[++j] = nums[i];
}
}
return ++j;
}
}
有两个点需要注意:
- 因为重复的可能有多个,所以不能以相等来做判定条件
- 注意
j++和++j的区别,此处用法很巧妙,也很必要!
LeetCode & Q26-Remove Duplicates from Sorted Array-Easy的更多相关文章
- Leetcode 26. Remove Duplicates from Sorted Array (easy)
Given a sorted array, remove the duplicates in-place such that each element appear only once and ret ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>
LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)
https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...
- [array] leetCode-26. Remove Duplicates from Sorted Array - Easy
26. Remove Duplicates from Sorted Array - Easy descrition Given a sorted array, remove the duplicate ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)
[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- [LeetCode] 26. Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
随机推荐
- RESTful接口设计原则和优点
RESTful架构优点: 前后端分离,减少流量 安全问题集中在接口上,由于接受json格式,防止了注入型等安全问题 前端无关化,后端只负责数据处理,前端表现方式可以是任何前端语言(android,io ...
- Java设计模式(一)工厂模式
一.场景描述 仪器数据文件的格式包含Pdf.Word.Excel等多种,不同种格式的文件其数据的采集方式不同,因此定义仪器数据采集接口,并定义PDF.Excel等不同的数据采集类实现该接口. 通过工厂 ...
- webpack学习(七)打包压缩图片
使用插件webpack-spritesmith生成雪碧图 1.安装webpack-spritesmith:npm install --save-dev webpack-spritesmith 2.配置 ...
- 分享一个命令行计算器-bc
分享一个命令行计算器-bc 假如你在一个图形桌面环境中需要一个计算器时,你可能只需要一路进行点击便可以找到一个计算器.例如,Fedora 工作站中就已经包含了一个名为 Calculator 的工具.它 ...
- Mycat 分片规则详解--ER关系表分片
ER模型是实体关系模型,基本元素是实体.关系和属性,Mycat 针对ER关系表的切分规则中,使得有相互依赖的表能够按照某一个规则切分到相同的节点上,避免垮库 Join 关系查询,下面的示例为订单(or ...
- Redis查询,设置超时时间
1.定义 Redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted ...
- 【Docker】 积累
■ 磁盘空间和docker资源之间的关系 通过docker info | grep Space可以看到Docker占据的磁盘空间的信息.其中Data Space Used表示实际上docker资源占用 ...
- centos文件系统变为只读的解决处理
简单粗暴:重启 Linux系统重启或无故变为只读造成网站无法正常访问的简单临时的做法: 一. 1.mount: 用于查看哪个模块输入只读,一般显示为: /dev/hda1 on / type ext3 ...
- 数据库 --> 8种NoSQL数据库对比
8 种 NoSQL 数据库对比 NoSQL是一项全新的数据库革命性运动,NoSQL的拥护者们提倡运用非关系型的数据存储.现今的计算机体系结构在数据存储方面要求具备庞大的水平扩展性,而NoSQL致力于改 ...
- 由浅入深理解----java反射技术
java反射机制详解 java反射机制是在运行状态下,对任意一个类可以获取该类的属性和方法,对任意一个对象可以调用其属性和方法.这种动态的获取信息和调用对象的方法的功能称为java的反射机制 clas ...