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 ...
随机推荐
- CRM客户关系管理系统(五)
第五章.分页功能开发 5.1.修改BaseKingAdmin和完善前段页面显示 现在访问没有注册的model会报错,因为基类中没有写list_display和list_filter. 在基类中设置一个 ...
- LintCode题解之判断是否为平方数之和
简单粗暴 public class Solution { /* * @param : the given number * @return: whether whether there're two ...
- AutoMagic-开源自动化平台构建思路
最近在github上看到AutoMagic自动化平台开源了,一时手痒,就试着搭了一套环境,现在把思路和大家说一说. AutoMagic从其工作分工分两部分: 1:Web端管理平台 管理平台基于Pyth ...
- webpack4.x配置详解,多页面,多入口,多出口,新特性新坑!!
花了差不多一天多的时间,重新撸了一遍webpack4.x的常用配置. 基本上常用的配置都熟悉了一遍,总体上来讲,为了对parcel进行反击,webpack从4.x开始,正在朝着尽可能的简化配置文件的方 ...
- 【Android应用开发】RecycleView API 翻译 (文档翻译)
. RecyclerView extends ViewGroupimplements ScrollingView NestedScrollingChild java.lang.Object ↳ ...
- linux下内存的统计和内存泄露类问题的定位
在产品的开发中,通过对当前系统消耗内存总量的统计,可以对产品所需内存总量进行精确的评估,从而选择合适的内存芯片与大小,降低产品的成本.在遇到内存泄露类问题时,经常会对此束手无策,本文通过对proc下进 ...
- Objective-C语法概述
Objective-C语法概述 简称OC 面向对象的C语言 完全兼容C语言 可以在OC里面混入C/C++代码 可以开发IOS和Mac OS X平台应用 语法预览 关键字 基本上都是以@开头(为了与C语 ...
- Detailed Item Cost Report (XML) timed out waiting for the Output Post-processor to finish
In this Document Symptoms Cause Solution References APPLIES TO: Oracle Cost Management - Ver ...
- popupwindow中EditText获取焦点后自动弹出软键盘
关于popupwindow中EditText获取焦点后自动弹出软键盘的问题,玩过手机qq或空间的童鞋应该知道,再点击评论时会弹出一个编辑框,并且伴随软键盘一起弹出是不是很方便啊,下面我们就来讲一下实现 ...
- Dynamics CRM2016 新功能之从CRM APP中导出数据至EXCEL
新版的CRM对移动端做了很多的改进,这归咎于微软对APP端的越来越重视.自己装了个IOS版的APP,体验了下基本的功能,比原来好用很多很顺滑,这里要介绍的是一个新的数据导出功能. 咱们进入case列表 ...