java-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 O(n) complexity.

乱序数组中找出最长连续序列 只能用O(N)时间复杂度 ,先排序再查找需要O(NlogN)显然不满足 线性时间查找只有用 哈希表 。再建立一个栈 每次在栈空时从数组中依序赋值com=num[index] 哈希表中删除此项 再用哈希查找 com-1和com+1若有则计数+1 哈希表中删除此项 并且将其加入队列 继续查找 代码如下:

public class Solution {
    public int longestConsecutive(int[] num) {
        if (num.length <= 1)
			return num.length;
		Map<Integer, Integer> hm = new HashMap<Integer, Integer>();
		for (int i = 0; i < num.length; i++) {
			if (!hm.containsKey(num[i])) {
				hm.put(num[i], 1);
			}
		}
		Queue<Integer> que = new LinkedList<Integer>();
		que.offer(num[0]);
		hm.remove(num[0]);
		int index = 1;
		int max = 1;
		int count = 1;
		while (index <=num.length - 1) {
			if (que.isEmpty()) {
				if (hm.containsKey(num[index])) {
					que.offer(num[index]);
					hm.remove(num[index]);
					index++;
				} else {
					index++;
				}
				if(count!=1){
				   max=Math.max(max, count);
				   count=1;
				}
			} else {
				int com = que.poll();
				if (hm.containsKey(com - 1)) {
					hm.remove(com - 1);
					que.offer(com - 1);
					count++;
				}
				if (hm.containsKey(com + 1)) {
					hm.remove(com + 1);
					que.offer(com + 1);
					count++;
				}
			}
		}
		return max;
    }
}
时间: 2024-09-06 22:17:28

java-Longest Consecutive Sequence的相关文章

[Leetcode][JAVA] 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

Longest Consecutive Sequence leetcode java

题目: 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 r

LeetCode: 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.

[Lintcode]124. Longest Consecutive Sequence/[Leetcode]128. Longest Consecutive Sequence

124. Longest Consecutive Sequence/128. Longest Consecutive Sequence 本题难度: Medium/Hard Topic: Data Structure Description Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Example Given [100, 4, 200, 1,

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