Leetcode_26_Remove Duplicates from Sorted Array
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41558551
Remove Duplicates from Sorted Array
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 in place with constant memory.
For example,
Given input array A = [1,1,2],
Your function should return length = 2, and A is now [1,2].
思路:
(1)这道题其实很简单。主要是因为数组已经是排好顺序的。如果不仔细看题目,把数组当作无序数组进行操作,OJ时会显示超时。
(2)题目要求是不能申请二额外空间,如果提交时有申请额外空间,也是不通过的。
(3)还需要注意的一个地方是,数组中元素的位置不能改变。比如对于数组[1,1,1,4,5],移除重复元素后为[1,4,5],起始数字为1,而不能是其它数字。
(4)我们只需对对组遍历一次,并设置一个计数器,每当遍历前后元素不相同,计数器加1,并将计数器对应在数组中位置定位到当前遍历的元素。
算法代码实现如下:
public static int removeDuplicates(int[] A) {
int len = A.length;
if (len == 0)
return 0;
int count = 1;
for (int i = 1; i < len; i++) {
if (A[i] == A[i - 1]) {
continue;
}else{
A[count] = A[i];
count++;
}
}
return count;
}
上面的解法是针对有序数组,如果是无序数组,应该如何解答?
思路:
(1)如果不允许申请额外空间,则可以先对数组进行排序,为了提高效率一般考虑使用快速排序,然后再参照上面有序数组进行操作;
(2)如果允许申请空间,则只需创建一个HashSet,遍历一次数组,通过contanins()方法进行判断就能得到结果。
(1)和(2)所对应代码如下所示(注:针对本文所示的题目,如果用下面代码进行OJ,(1)会超时,(2)会产生额外空间):
不可以申请额外空间:
public static int removeDuplicates(int[] A) {
int len = A.length;
if (len == 0)
return 0;
quickSort(A, 0, len - 1);
int count = 1;
for (int i = 1; i < len; i++) {
if (A[i] == A[i - 1]) {
continue;
} else {
A[count] = A[i];
count++;
}
}
return count;
}
//快速排序
private static void quickSort(int[] table, int low, int high) {
if (low < high) {
int i = low, j = high;
int vot = table[i];
while (i != j) {
while (i < j && vot <= table[j])
j--;
if (i < j) {
table[i] = table[j];
i++;
}
while (i < j && table[i] < vot)
i++;
if (i < j) {
table[j] = table[i];
j--;
}
}
table[i] = vot;
quickSort(table, low, j - 1);
quickSort(table, i + 1, high);
}
}
可以申请额外空间:(其中,HashSet的contains()方法是用来过滤重复元素的)
public static int removeDuplicates(int[] A) {
int len = A.length;
HashSet<Integer> set = new HashSet<Integer>();
for (int i = 0; i < len; i++) {
if (set.size() == 0) {
set.add(A[i]);
}
if (!set.contains(A[i])) {
set.add(A[i]);
}
}
return set.size();
}
Leetcode_26_Remove Duplicates from Sorted Array的更多相关文章
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- Remove Duplicates From Sorted Array
Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 26. Remove Duplicates from Sorted Array
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
- LeetCode:Remove Duplicates from Sorted Array I II
LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...
- 【26】Remove Duplicates from Sorted Array
[26]Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...
- leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
随机推荐
- Windows无法安装到这个磁盘
今天手动装系统的时候出现以下这样的错误, 请看图: 进入BIOS F9 Setup Defaults ,初始化恢复 1.在进行windows安装分区时, 磁盘分区界面无法继续进行,出现" ...
- Docker rancher 部署
Docker-rancher #环境 centos7.4 , Docker version 17.12.0-ce #下载docker镜像 docker pull mysql:5.7 docker pu ...
- Java第8次实验(IO流)
参考资料 本次作业参考文件 正则表达式参考资料 第1次实验 1. 字符流与文本文件:使用 PrintWriter(写),BufferedReader(读) 参考文件:基础代码目录Student.jav ...
- CSDN没有审核投诉的真实性直接删除博主上传的资源
今天打开博客,发现一条未读通知:您上传的资源* * * *因质量投诉没有通过审核,如有疑问,请联系webmaster@csdn.net 我马上去看了下我的资源下载页,资源已经被删除,积分也已清空- ...
- Objective-C 中如何测量代码的效率
背景 在我们编程的时候,可能经常会有一些疑问: * 我们写的某个方法的执行效率是多少? * 方法 A 和 方法 B 哪个更快? 因此,我们不可避免的要用到一些方法来计算代码的执行效率.计算代码的执行效 ...
- Java并发框架——AQS中断的支持
线程的定义给我们提供了并发执行多个任务的方式,大多数情况下我们会让每个任务都自行执行结束,这样能保证事务的一致性,但是有时我们希望在任务执行中取消任务,使线程停止.在java中要让线程安全.快速.可靠 ...
- lucene索引库的增删改查操作
1. 索引库的操作 保持数据库与索引库的同步 说明:在一个系统中,如果索引功能存在,那么数据库和索引库应该是同时存在的.这个时候需要保证索引库的数据和数据库中的数据保持一致性.可以在对数据库进行增.删 ...
- Cartographer资料分享
中文资料稍后补充 Introducing Cartographer By Tully Foote on October 5, 2016 10:11 AM From Damon Kohler, Wolf ...
- Android获取当前网络状态
Android获取当前网络状态 效果图 有网络 没有网络 源码 下载地址(Android Studio工程):http://download.csdn.net/detail/q4878802/9052 ...
- little kernel中如何决定app目录下应该包含哪个app
lk中是会为每个app建立一个thread,所以的app都是放在app这个路径下,那是在哪里决定的呢?一般是通过在project下面的MODULE决定的,例如下面这个例子就只用app下面的aboot这 ...