Longest Consecutive Sequence

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43854597

Given an unsorted array of integers, find the length of the longest consecutive elements sequence.

For example,

Given [100, 4, 200, 1, 3, 2],

The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.

Your algorithm should run in O(n) complexity.

思路:

(1)题意为给定一个未排序的数组,求数组中最长连续元素的个数。

(2)由于数组是未排序的,而需要求得数组中连续序列元素的个数。首先想到的应该是对数组进行排序,本文用的是java类库中自带的排序算法:Arrays.sort()。然后遍历数组,由于数组中可能出现多个连续序列,所以设置当前最长序列个数max和正在遍历的连续序列的个数count。首先,从数组下标为0开始往后遍历,判断当前元素和后续元素是否相差1,如果相差1,则count++,当遍历到倒数第二个元素时,返回count和max的较大值;如果相等,判断max和count大小,将较大值赋给max,继续遍历;其余情况为值相差大于1,这时将max和count较大值赋给max,并将count置为1(因为连续序列在这里断开了,需要重新记录);遍历完整个数组,所得max即为最长连续序列个数。

(3)本文算法效率不是很高,有待后续优化。希望本文对你有所帮助。

算法代码实现如下:

/**
 * @author liqq
 */
public class Longest_Consecutive_Sequence {
	public int longestConsecutive(int[] num) {
		if (num == null)  return -1;
		if (num.length == 1)  return 1;

		Arrays.sort(num);

		int count = 1;
		int max = 1;
		for (int i = 0; i < num.length - 1; i++) {
			if (num[i] + 1 == num[i + 1]) {
				count = count + 1;
				if (i == num.length - 2) {
					return count > max ? count : max;
				}
			} else if (num[i] == num[i + 1]) {
				max = count > max ? count : max;
				continue;
			} else {
				max = max > count ? max : count;
				count = 1;
			}

		}
		return max;
	}
}
时间: 2024-11-08 22:28:14

Longest Consecutive Sequence的相关文章

LeetCode OJ - Longest Consecutive Sequence

这道题中要求时间复杂度为O(n),首先我们可以知道的是,如果先对数组排序再计算其最长连续序列的时间复杂度是O(nlogn),所以不能用排序的方法.我一开始想是不是应该用动态规划来解,发现其并不符合动态规划的特征.最后采用类似于LRU_Cache中出现的数据结构(集快速查询和顺序遍历两大优点于一身)来解决问题.具体来说其数据结构是HashMap<Integer,LNode>,key是数组中的元素,所有连续的元素可以通过LNode的next指针相连起来. 总体思路是,顺序遍历输入的数组元素,对每个

Binary Tree Longest Consecutive Sequence

Given a binary tree, find the length of the longest consecutive sequence path. The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The longest consecutive path need to be from p

Longest Consecutive Sequence 数组连续数字的情况

Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4

【leetcode刷题笔记】Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run in

leetcode --day12 Surrounded Regions &amp; Sum Root to Leaf Numbers &amp; Longest Consecutive Sequence

1.  Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region. For example, X X X X X O O X X X O X X O X X After running your fu

Binary Tree Longest Consecutive Sequence Leetcode

Given a binary tree, find the length of the longest consecutive sequence path. The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The longest consecutive path need to be from p

Leetcode 贪心 Longest Consecutive Sequence

Longest Consecutive Sequence Total Accepted: 19169 Total Submissions: 68303My Submissions Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consec

[LeetCode] Longest Consecutive Sequence(DP)

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run in

LeetCode——Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run i

[leetcode]Longest Consecutive Sequence @ Python

原题地址:https://oj.leetcode.com/problems/longest-consecutive-sequence/ 题意: Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements seque