LeetCode之longst 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)。这题难度不高,我直接先排序再查找。给出三种解法,第一种解法是我自己设计,后面两种是参考网上比较好的解法,学习学习。

代码:

<span style="font-size:14px;">#include "stdafx.h"
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <iostream>
using namespace std;

class Solution{
public:
	//时间O(n)
	int longstConsecutiveSequence(vector<int> &num){
		//先排序
		sort(num.begin(), num.end(),less<int>());

		int max = 1; //记录最长的连续个数
		int n = 1; //记录单次连续的个数
		bool flag = false; //标识是否连续
		int temp = -1; //记录前面一个元素
		vector<int>::const_iterator cit;
		for (cit = num.begin();cit != num.end();cit++){
			if (flag){
				if (*cit == (temp + 1)){
					n++;
				}else{
					flag = false;
					if (n>max){
						max = n;
					}
					n = 1;
				}
			}else{
				flag = true;
				if (*cit == (temp + 1)){
					n++;
				}
			}
			temp = *cit;
		}
		//可能最后一个连续的最大
		if (n>max){
			max = n;
		}
		return max;
	}

	int longstConsecutiveSequence2(vector<int> &num){
		unordered_map<int, bool> used;
		vector<int>::iterator it;
        //初始化,赋值到map中
		for (it = num.begin();it != num.end();it++) used[*it] = false;
		int longest=0;
		for (it = num.begin();it != num.end();it++){
			if (used[*it]) continue;
			int length = 1;
			used[*it] = true;

			//向两边查找
			for (int j = *it + 1; used.find(j) != used.end(); ++j) {
				used[j] = true;
				++length;
			}
			for (int j = *it - 1; used.find(j) != used.end(); --j) {
				used[j] = true;
				++length;
			}
			//取最大的
			longest = max(longest, length);
		}
		return longest;
	}

	int longstConsecutiveSequence3(vector<int> &num){
		unordered_map<int, int> map;
		int size = num.size();
		int l = 1;

		for (int i = 0; i < size; i++) {
			if (map.find(num[i]) != map.end()) continue;
			map[num[i]] = 1;
			if (map.find(num[i] - 1) != map.end()) {
				l = max(l, mergeCluster(map, num[i] - 1, num[i]));
			}
			if (map.find(num[i] + 1) != map.end()) {
				l = max(l, mergeCluster(map, num[i], num[i] + 1));
			}
		}
		return size == 0 ? 0 : l;
	}

private:
	int mergeCluster(unordered_map<int, int> &map, int left, int right) {
		int upper = right + map[right] - 1;
		int lower = left - map[left] + 1;
		int length = upper - lower + 1;
		map[upper] = length;
		map[lower] = length;
		return length;
	}
};

int _tmain(int argc, _TCHAR* argv[])
{
	vector<int> a;
	a.push_back(5);a.push_back(6);
	a.push_back(1);a.push_back(2);a.push_back(3);
	a.push_back(22);a.push_back(23);a.push_back(24);a.push_back(25);
	a.push_back(7);a.push_back(100);

	Solution s;
	int rtn = s.longstConsecutiveSequence2(a);

	cout << rtn << endl;
	system("pause");
	return 0;
}</span>

个人感觉第二种比较好,易于理解。

今天刷了两题LeetCode,花了好长时间,瞬间感觉资质太平庸了。。。。。

时间: 2024-08-26 18:43:40

LeetCode之longst Consecutive Sequence的相关文章

LeetCode OJ - Longest Consecutive Sequence

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

【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笔记] 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 al

【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

[LeetCode][JavaScript]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 贪心 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(hard)☆

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

Java for LeetCode 128 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