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 ...
随机推荐
- python框架Scrapy中crawlSpider的使用
一.创建Scrapy工程 #scrapy startproject 工程名 scrapy startproject demo3 二.进入工程目录,根据爬虫模板生成爬虫文件 #scrapy genspi ...
- 简单工厂模式设计(java反射机制改进)
如果做开发的工作,工厂设计模式大概都已经深入人心了,比较常见的例子就是在代码中实现数据库操作类,考虑到后期可能会有数据库类型变换或者迁移,一般都会对一个数据库的操作类抽象出来一个接口,然后用工厂去获取 ...
- 在Sql Server中使用证书加密数据
IF NOT EXISTS () CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'QWE23987zxJKL569&agf1$94467GRkjg5k3 ...
- JS练习--自动生成100个li
点击按钮,自动生成100个li,红.黄.蓝.绿四种颜色的顺序显示出现在页面中 CSS: ;;} ul,li{list-style: none;} #ul1{position: relative;} # ...
- WSGI基础知识(转)
add by zhj: WSGI全称Web Server Gateway Interface,即Web网关接口.其实它并不是OSI七层协议中的协议,它就是一个接口(即函数)而已,而WSGI规定了该接口 ...
- Linux内核学习资料
1.为什么计算机的学生要学习Linux开源技术 http://tinylab.org/why-computer-students-learn-linux-open-source-technologie ...
- MySQL数据库(2)- 库的操作、表的操作、数据的操作、存储引擎的介绍
一.库的操作 1.系统数据库 执行如下命令,查看系统数据库: mysql> show databases; 参数解释: information_schema: 虚拟库,不占用磁盘空间,存储的是数 ...
- JIRA 模块 bug管理工具
from jira import JIRA #导入jira jira=JIRA(server='http://127.0.0.1:8080', basic_auth=('name', 'passwor ...
- Linux学习笔记(4)磁盘分区(fdisk)、挂载与文件系统命令
Linux学习笔记(4)磁盘分区(fdisk).挂载与文件系统命令 1.磁盘分区是怎么表示的? 1.1 对于IDE接口,第一主盘为hda,第1从盘为hdb,第1从盘的第1个分区为hdb1 1.2 对于 ...
- App doesn't auto-start an app when booting the device in Android
From Android 3.1, newly installed apps are always put into a "stopped" state and the only ...