LeetCode第[26]题(Java):Remove Duplicates from Sorted Array 标签:Array
题目难度:Easy
题目:
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 by modifying the input array in-place with O(1) extra memory.
翻译:
给定一个排好序的数组后,删除重复的元素,这样每个元素只出现一次,并返回新的长度。
不要新建另一个数组分配额外的空间,只能通过修改原有数组,且空间复杂度为O(1)。
示例:[1,1,2]——[1,2] 2
思路:一看见消除重复,就想到了Set,然后写了代码如下
public int removeDuplicates(int[] nums) {
Set s = new HashSet();
for (int i = 0; i < nums.length; i++) {
s.add(nums[i]);
}
return s.size();
}
但是答案说错误??
因为此题比较特殊,它不仅仅检查最后的结果,而且检查nums最后的值是否是期望的(只截取最后返回结果的大小)
然后我在后面加了个迭代器循环赋值,结果还是不对?
for (Iterator iterator = s.iterator(); iterator.hasNext();) {
nums[i++] = (Integer) iterator.next();
}

顺序也要管? 好吧HashSet无序的,那就用TreeSet吧:
public int removeDuplicates(int[] nums) {
Set<Integer> s = new TreeSet<Integer>();
for (int i = 0; i < nums.length; i++) {
s.add(nums[i]);
}
int i = 0;
for (Iterator iterator = s.iterator(); iterator.hasNext();) {
nums[i++] = (Integer) iterator.next();
}
return s.size();
}
161 / 161 test cases passed. Status: Accepted Runtime: 24 ms beats 5.31%
由于此处采用Set占用了额外的空间,空间复杂度为O(N),虽然结果正确,但是不符合原题意图,下面是参考答案。
public int removeDuplicates(int[] nums) {
if (nums.length == 0) return 0;
int i = 0;
for (int j = 1; j < nums.length; j++) {
if (nums[j] != nums[i]) {
i++;
nums[i] = nums[j];
}
}
return i + 1;
}
一开始有往这方面想,但是运行后发现边界问题总是处理不了,就放弃了,
此处巧妙地采用了一个循环外的int做指针对原数组进行修改,同时循环内部从第二个开始与指针所指做比较,跳过重复的元素。

期间编译错误:
1. if 后面的括号内的判断“==”写成了“=”;
2. Set的toArray方法只能利用传参toArray( new int[set.size()] ) 这样才不会有转型错误,但是这样也只能转为非基本类型(Integer而不能是nt),像转为int数组只能利用迭代器进行循环赋值;
3. 忘记写return。
LeetCode第[26]题(Java):Remove Duplicates from Sorted Array 标签:Array的更多相关文章
- LeetCode专题-Python实现之第26题:Remove Duplicates from Sorted Array
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- 【LeetCode每天一题】Remove Duplicates from Sorted Array II(移除有序数组中重复的两次以上的数字)
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- 【LeetCode每天一题】Remove Duplicates from Sorted List(移除有序链表中的重复数字)
Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1 ...
- 算法题丨Remove Duplicates from Sorted Array II
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? 示例 Giv ...
- 算法题丨Remove Duplicates from Sorted Array
描述 Given a sorted array, remove the duplicates in-place such that each element appear only once and ...
- 【Leetcode】【Medium】Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode][Java] Remove Duplicates from Sorted List II
题意: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct ...
- leetcode修炼之路——83. Remove Duplicates from Sorted List
哈哈,我又来了.昨天发现题目太简单就没有放上来,今天来了一道有序链表的题.题目如下: Given a sorted linked list, delete all duplicates such th ...
- 【Leetcode】【Easy】Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
随机推荐
- sql---如何把sql查询出来的结果当做另一个sql的条件查询,1、语句2、with as
'; -- table2 的 name 作为 table1的条件 select * from table1 where name in (select name from table2) --如果有多 ...
- shader常用
1 模型空间转裁剪空间 UnityObjectToClipPos(v.vertex) 2 模型空间转世界空间 mul( unity_ObjectToWorld, v.vertex ) 3 雾三件套 U ...
- json的相关操作
最近对json的操作不是很理解 定义: JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式. 它基于 ECMAScript (w3c制定的j ...
- [DevOps] 认识一下
大家都在说DevOps(Develop Operation),大概知道就是开发和运维沟通交流,一条线,然后使产品能够顺利的.短时间内上线.维稳什么的. 今天特意看了下 DockOne里面的一篇文章,再 ...
- STL 中的链表排序
一直以来学习排序算法, 都没有在链表排序上下太多功夫,因为用得不多.最近看STL源码,才发现,原来即使是链表,也能有时间复杂度为O(nlogn)的算法, 大大出乎我的意料之外,一般就能想到个插入排序. ...
- vue之 node.js 的简单介绍
一.什么是 node.js? 它是可以运行在JavaScript的服务平台 二.安装 1.node.js的特性 - 非阻塞IO模型 - 时间驱动 2.运用场景 - 高并发低业务 - 实时场景 - 聊天 ...
- 解决hash冲突的办法
1.开发定址法 2.再哈希法 3.链地址法 4.建立一个公共溢出区
- Java集合(8):Hashtable
一.Hashtable介绍 和HashMap一样,Hashtable 也是一个散列表,它存储的内容是键值对(key-value)映射,它在很大程度上和HashMap的实现差不多. Hashtable ...
- mongodb-2.6.0 在win7 64下的安装和服务启动
转自: http://blog.csdn.net/lingchen214/article/details/24537629 1 自定义安装到C:\mongodb目录下. 2 手动在C:\mong ...
- LLServer--》对LevelDB的应用
http://code.google.com/p/llserver/ 查看libs path的路径 LD_DEBUG=libs /usr/bin/llserver -h