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 ...
随机推荐
- windows 全角 怎么切换到半角
windows 全角 怎么切换到半角 :shift+空格键
- MySQL日期时间字段
mysql支持的日期时间类型有:DATETIME. TIMESTAMP.DATE.TIME.YEAR. 几种类型比较如下: DATETIME DATETIME 用于表示 年月日 时分秒,是 DATE和 ...
- IDEA 录制宏+设置快捷键 实现写时编译
参考: IDEA 录制宏+设置快捷键 实现写时编译:https://blog.csdn.net/wangjie919/article/details/79487981 IDEA 设置运行时不编译项目: ...
- SQL 将列转成字符串并用逗号分隔
SELECT STUFF((SELECT ',' + FieldName FROM TableName FOR XML PATH('')),1,1,'') AS T 其中的逗号可以换成其它字符 转换完 ...
- Redis2.8配置文件详解(转)
add by zhj : 没找到本文的原文.另外,redis配置文件中文翻译 也翻译的不错,可以与本文对照看.两篇文章都是以Redis2.8来介绍的 在Redis中直接启动redis-server服务 ...
- java 多线程 day03 线程同步
package com.czbk.thread; /** * Created by chengtao on 17/12/3. 线程安全问题: 线程安全出现 的根本原因: 1. 存在两个或者两个以上 的 ...
- xcode中全文查询某个中文字
查询所有中文 [^"]*[\u4E00-\u9FA5]+[^"\n]*? 查询某个中文字“中”字 [^"]*[\u4e2d]+[^"\n]*? 中文字转成uni ...
- MySQL 单表查询(Day42)
阅读目录 一,查询语法 二,简单查询 三,where约束 四,having过滤 五,分组查询 group by 六,关键字的执行优先级 七,查询排列 order by 八,使用聚合函数查询 九,whe ...
- Excel数据常用操作,vlookup,text,trim,数据格式导致出错
数据有缺漏,需要在数据前面补零 =TEXT(F70,"000000") 前面是要操作的数据,后面是补几位 匹配数据(将一个表格中的数据进行匹配) =VLOOKUP(C2,aaa,4 ...
- flex 客户端缓存SharedObject
读取缓存: usernameSO = SharedObject.getLocal('username'); if (usernameSO) { usernameSOAL = usernameSO.da ...