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 ...
随机推荐
- C++ 调用webservice 出现 函数返回值为 3 (SOAP_TAG_MISMATCH) 的解决方案
最近在用C++ gsoap做webservice服务时,函数返回值为SOAP_TAG_MISMATCH (==3)错误码,原因是我传入wsdl地址时连同后面的?wsdl都传入了,如下: http:// ...
- iOS核心动画详解(CABasicAnimation)
前言 上一篇已经介绍了核心动画在UI渲染中的位置和基本概念,但是没有具体介绍CAAnimation子类的用法,本文将介绍CABasicAnimation及其子类CASpringAnimation的用法 ...
- <2014 08 28> 大学学习小结
大一:哲学.物理(科普).瞎玩 大二:机械.力学.继续哲学 大三:电子电路.计算机.编程 大四:毕业项目(机器人等) 研一:物理.数学(常熟) 研二:AGV.TUM实习 研三:写论文.实习程序员.申请 ...
- <2014 04 29> *nix环境编程常用库总结
-------------------------linux常用头文件如下:POSIX标准定义的头文件<dirent.h> 目录项<fcntl.h> ...
- Django HttpResponse对象详解
HttpResponse对象 Django服务器接收到客户端发送过来的请求后,会将提交上来的这些数据封装成一个HttpRequest对象传给视图函数.那么视图函数在处理完相关的逻辑后,也需要返回一个响 ...
- 【我的Android进阶之旅】解决MediaPlayer播放音乐的时候报错: Should have subtitle controller already set
一错误描述 二错误解决 解决方法一 解决方法二 一.错误描述 刚用MediaPlayer播放Music的时候,看到Log打印台总是会打印一条错误日志,MediaPlayer: Should have ...
- CentOS 6.5 QtCreator启动时 dbus-1的错误解决方法
启动QtCreator提示:dbus_connection_can_send_type的错误, QString::arg: Argument missing: 无法解析dbus_connection_ ...
- 编程语言的介绍(Day2)
1.什么是编程,为什么要编程? 编程==编写程序(写代码) 2.编程语言有哪些 机器语言 优点是最底层,速度最快,缺点是最复杂,开发效率最低 汇编语言 优点是比较底层,速度最快,缺点是复杂,开发效率最 ...
- Uber中国在地方城市的人员架构是怎样的?
http://www.thepaper.cn/newsDetail_forward_1390516 澎湃新闻:Uber中国在地方城市的人员架构是怎样的? 柳甄:一般是3人组成的小团队作战.一名城市 ...
- python中的pass语句是什么
当用python写代码时,有时可能还没想好函数怎么写,只写了函数声明,但为了保证语法正确,必须输入一些东西,这种情况下,我们会使用pass语句 def func(*args): pass break语 ...