本文是在学习中的总结,欢迎转载但请注明出处: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. Node.js 常用工具util

    util 是一个Node.js 核心模块,提供常用函数的集合,用于弥补核心JavaScript 的功能 过于精简的不足. util.inherits util.inherits(constructor ...

  2. PHP Libxml 函数

    PHP Libxml 简介 Libxml 函数和常量与 SimpleXML.XSLT 以及 DOM 函数一起使用. 安装 这些函数需要 Libxml 程序包. 在 xmlsoft.org 下载 PHP ...

  3. PHP 5 Calendar 函数

    PHP Calendar 简介 日历扩展包含了简化不同日历格式间的转换的函数. 它是基于 Julian Day Count(儒略日计数),是从公元前 4713 年 1 月 1 日开始计算的. 注释:如 ...

  4. Docker控制组

    控制组是 Linux 容器机制的另外一个关键组件,负责实现资源的审计和限制. 它提供了很多有用的特性:以及确保各个容器可以公平地分享主机的内存.CPU.磁盘 IO 等资源:当然,更重要的是,控制组确保 ...

  5. Redis监控工具,命令和调优

    Redis监控工具,命令和调优 1.图形化监控 因为要对Redis做性能测试,发现了GitHub上有个python写的RedisLive监控工具评价不错.结果鼓捣了半天,最后发现其主页中引用了Goog ...

  6. 负载均衡LVS(DR模式)安装实战

    1.编译安装ipvsadm 首先从LVS官网下载tarball,解压后make && make install即可. 要注意的是LVS的依赖有:popt-static.libnl.ke ...

  7. CSAPP缓冲区溢出攻击实验(下)

    CSAPP缓冲区溢出攻击实验(下) 3.3 Level 2: 爆竹 实验要求 这一个Level的难度陡然提升,我们要让getbuf()返回到bang()而非test(),并且在执行bang()之前将g ...

  8. Redis源码剖析--源码结构解析

    请持续关注我的个人博客:https://zcheng.ren 找工作那会儿,看了黄建宏老师的<Redis设计与实现>,对redis的部分实现有了一个简明的认识.在面试过程中,redis确实 ...

  9. Unity UGUI实现分段式血条

    我们可以看到像英雄联盟等游戏里英雄头顶的血条显示并非是纯色的,而是根据血量的多少而显示一定量的格子,这种方式明显是比较友好.比较美观的,事实上我们的游戏里面也想实现这样的效果,那该怎么办呢?根据血量的 ...

  10. Python 继承标准类时发生了什么

    定义标准类dict的一个子类c: >>> class c(dict): pass >>> y=c({1:2,3:4}) >>> y {1: 2, ...