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 ...
随机推荐
- 通过AIDL在两个APP之间Service通信
一.项目介绍 [知识准备] ①Android Interface definition language(aidl,android接口定义语言),其目的实现跨进程的调用.进程是程序在os中执行的载体, ...
- 110个oracle常用函数总结
. ASCII 返回与指定的字符对应的十进制数; SQL) zero,ascii( ) space from dual; A A ZERO SPACE --------- --------- ---- ...
- EXISTS的使用详解
.exists的使用场合: exists 用于只能用于子查询,可以替代in,若匹配到结果,则退出内部 查询,并将条件标志为true,传回全部结果资料,in 不管匹配到匹配不到都 全部匹配完毕,使用ex ...
- Docker 联合文件系统
联合文件系统(UnionFS)是一种分层.轻量级并且高性能的文件系统,它支持对文件系统的修改作为一次提交来一层层的叠加,同时可以将不同目录挂载到同一个虚拟文件系统下(unite several dir ...
- Java第7次实验提纲(多线程)
PTA与参考资料 题集:多线程 多线程实验参考文件 ThreadReading 实验-基础部分 1.1 基础题目MyThread类.自行完成题集合的:PrintTask 1.2 Runnable与匿名 ...
- 加解密、PKI与CA基础
介绍 这门知识如果以前尝过的各位想必都知道:枯燥无比!因此在文中我会尽量讲的生动些,举一些例子,并试图以一个完整的例子来贯穿整个讲述过程.今年又恰逢莎翁逝世400周年,一方面也为了纪念这位伟大的作家. ...
- 利用git pull的勾子实现敏捷部署
监听端 例如nginx或Python,php,rails等后端 git --git-dir=~/op/.git --work-tree=~/op pull git hooks端 位于.git/hook ...
- 20160217.CCPP体系详解(0027天)
程序片段(01):TestCmd.c 内容概要:管道_字符串 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include < ...
- 剑指Offer——关于劳动合同,这6件事毕业生必须知道!
剑指Offer--关于劳动合同,这6件事毕业生必须知道! 求职找工作,不少人拿到劳动合同的那刻,可能连合同内容都没看清,就挥着笔杆子"签签签".别急!劳动合同包含哪些条款你清楚 ...
- 用scheme最基本的元素定义排序函数
用到的元素有9个: define,if,null?,cons car,cdr,lambda,let,named let, 其实let 和 named let可以去掉.但那样会带来性能和可读性下降的问题 ...