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 ...
随机推荐
- golang 开发过程中的坑
1. chan数据读取写入 正常情况下chan读取写入都没有问题,但是如果chan关闭之后会出现问题 所以读取chan数据的时候需要增加chan是否关闭的判断 c := make(chan ) v, ...
- C# 利用StringBuilder提升字符串拼接性能
一个项目中有数据图表呈现,数据量稍大时显得很慢. 用Stopwatch分段监控了一下,发现耗时最多的函数是SaveToExcel 此函数中遍列所有数据行,通过Replace替换标签生成Excel行,然 ...
- Duilib 入门级教程 推荐
http://www.cnblogs.com/Alberl/category/520438.html 作者写的不错,图文并茂,适合刚入门.
- 实践中需要了解的cpu特性
目录 分段机制 特权级检查 GDT和LDT 堆栈切换 分页机制 中断 分段机制 实模式中cs是一个实实在在的段首地址,ip为cs所指向段的偏移,所以cs<<4+ip是当前cpu执行的指令. ...
- jquery获取浏览器类型和版本号的方法
$(document).ready(function(){ varbrow=$.browser; varbInfo=""; if(brow.msie){bInfo="Mi ...
- 【我的Android进阶之旅】推荐一款能提升数十倍效率的Android应用开发助手
一功能介绍 a调试相关 1布局边界 2布局更新 3强制GPU渲染 4GPU渲染 5指针位置 6严格模式 7不保留应用 8不锁定屏幕 9开发者选项 10系统设置 11语言设置 12USB调试 b UI相 ...
- MySQL数据库(4)- 多表查询、可视化工具Navicat的使用、设计模式MVC
一.多表查询 准备工作:创建两张表,部门表(department).员工表(employee),代码和表格如下: # 创建表 create table department( id int, name ...
- Android学习十---Android Camera
Android camera用来拍照和拍摄视频的先看一下最后实现的效果图 最后的效果图 一.准备 在你的应用程序上使用android拍照设备,需要考虑以下几个方面 1. 是否是 ...
- shell脚本调用传参【转载】
转自:https://www.cnblogs.com/cisum/p/8010658.html 1.直接使用$0,$1,$2,$3 $0是脚本的名字,就是按顺序来 #!/bin/bash # auth ...
- linux 或c 时间相关处理类型和函数
注意1.精确级别,纳秒级别原型long clock_gettime (clockid_t which_clock, struct timespec *tp); 头文件time.hwhich_cloc ...