本文是在学习中的总结,欢迎转载但请注明出处: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();
}

最新文章

  1. 理解OVER子句
  2. [转]Windows7文件夹转移清理臃肿的C盘
  3. 写给Git初学者的7个建议
  4. MySQL数据迁移到MSSQL-以小米数据库为例-测试828W最快可达到2分11秒
  5. Caffe源码解析4: Data_layer
  6. 小程序de 一些经验1
  7. CEF3开发者系列之CEF3入门
  8. [LintCode] Copy Books 复印书籍
  9. ob_start()失效与phpunit的非正常结束
  10. 产生不重复的随机数TGUID
  11. POJ 3321 Apple Tree(树状数组)
  12. qt windows分发工具使用(windoployqt)
  13. grub配置文件grub.conf详细说明
  14. 使用POI生成Excel报表
  15. 数列分段Section II
  16. Spring MVC前后端的数据传输
  17. Java-IO流之File操作和Properties操作
  18. Spring(1)—初识
  19. Math的方法;Date的方法;
  20. gentoo virtualbox 无法启动

热门文章

  1. Linux完全卸载Oracle的操作步骤
  2. 阿里云linux下web服务器配置
  3. Docker rancher 部署
  4. Android开发学习之路--性能优化之常用工具
  5. lua 序列化函数
  6. Ubuntu下装QQ2014(http://my.oschina.net/oscfox/blog/315951)
  7. Android Multimedia框架总结(八)Stagefright框架之AwesomePlayer及数据解析器
  8. 使用Fresco实现简单的显示一张图片
  9. 指令汇B新闻客户端开发(三) 下拉刷新
  10. Ubuntu下配置Telnet服务器