本文是在学习中的总结,欢迎转载但请注明出处: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的更多相关文章

  1. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  2. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  3. Remove Duplicates From Sorted Array

    Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...

  4. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  5. 26. Remove Duplicates from Sorted Array

    题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  6. 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 ...

  7. LeetCode:Remove Duplicates from Sorted Array I II

    LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...

  8. 【26】Remove Duplicates from Sorted Array

    [26]Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...

  9. leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法

    Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...

随机推荐

  1. geotrellis使用(四十)优雅的处理请求超过最大层级数据

    前言 要说清楚这个题目对我来说可能都不是一件简单的事情,我简单尝试. 研究 GIS 的人应该都清楚在 GIS 中最常用的技术是瓦片技术,无论是传统的栅格瓦片还是比较新颖的矢量瓦片,一旦将数据切好瓦片就 ...

  2. Bootstrap3 表单-被支持的控件:文本域

    支持多行文本的表单控件.可根据需要改变 rows 属性. <textarea class="form-control" rows="3"></ ...

  3. Weblogic 12c 集群环境搭建

    本文是在windows7操作系统下配置的,jdk版本1.7 ,weblogic版本12.1.3.0.0. 搭建集群前的规划 其中AdminServer是总控制端,server1.server2.ser ...

  4. Android 多窗口详解

    多窗口支持 Android N 添加了对同时显示多个应用窗口的支持. 在手持设备上,两个应用可以在"分屏"模式中左右并排或上下并排显示. 在电视设备上,应用可以使用"画中 ...

  5. ROS机器人程序设计(原书第2版)补充资料 (伍) 第五章 计算机视觉

    ROS机器人程序设计(原书第2版)补充资料 (伍) 第五章 计算机视觉 书中,大部分出现hydro的地方,直接替换为indigo或jade或kinetic,即可在对应版本中使用. 计算机视觉这章分为两 ...

  6. PHP和MySQL Web开发学习笔记介绍

    前言 从2016年2月1日开始,之后的几个月左右的时间里,我会写一个系列的PHP和MySQL Web开发的学习笔记.我之前一直从事Java语言的开发工作,最近这段时间非常想学习一门语言,就选择了PHP ...

  7. Android开发指南--0 总览

    无意间发现一个网站,主打IOS方面的教程,然而作为一个Android开发者,我就找了下网站里有没有Android的教程,还真有,这里就翻译一下. 翻译目标教程:https://www.raywende ...

  8. Swift基础之守卫语句guard

    本篇文章翻译自:http://ericcerney.com/swift-guard-statement/原作者:ecerney该语法为swift2.0之后添加的新特性 最开始在Apple的Platfo ...

  9. javascript之内置函数

    1.常规函数 (1)alert函数:显示一个警告对话框,包括一个OK按钮. (2)confirm函数:显示一个确认对话框,包括OK.Cancel按钮. (3)escape函数:将字符转换成Unicod ...

  10. SSH深度历险(九) Struts2+DWZ+Uploadify实现多文件(文件和图片等等)上传

    在gxpt_uas系统中,要实现文件(文件和图片等等,可以灵活配置)的批量上传至mongodb,在学习这个过程中,学习了mongodb,并实现了批量上传的功能,实现思路:在DWZ的基础上参考官方的实例 ...