Leetcode : Longest Consecutive Sequence
2013-02-28 00:00
302 查看
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
For example,
Given
The longest consecutive elements sequence is
Your algorithm should run in O(n) complexity.
思路:
题目咋一看很简单,排个序然后从头到尾遍历一下就好,但是这种方法并不能解决该问题,关键是该题要求算法的复杂度是O(n),而排序最快也需要O(n*log(n))。那么只能用空间换时间了,所以很自然就想到了Hash。(之前自以为对Hash学的很好,直到用的时候才发现其实Hash还不存在我的思维中)。
下面是我的代码,其中用到了std::unordered_map(Unordered associative containers implement unsorted (hashed) data structures that can be quickly searched (O(1)amortized,
O(n) worst-case complexity),它是C++11标准引入的东西,具体用法可参考http://en.cppreference.com/w/cpp/container/unordered_map
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(n*log(n))。那么只能用空间换时间了,所以很自然就想到了Hash。(之前自以为对Hash学的很好,直到用的时候才发现其实Hash还不存在我的思维中)。
下面是我的代码,其中用到了std::unordered_map(Unordered associative containers implement unsorted (hashed) data structures that can be quickly searched (O(1)amortized,
O(n) worst-case complexity),它是C++11标准引入的东西,具体用法可参考http://en.cppreference.com/w/cpp/container/unordered_map
int longestConsecutive(vector<int> &num) { if (num.empty()) return 0; if (1 == num.size()) return 1; unordered_map<int, int> num_map; for (vector<int>::iterator it = num.begin(); it != num.end(); ++it) { ++num_map[*it]; } int maxLen = 1; for (vector<int>::iterator it = num.begin(); it!= num.end(); ++it) { if (num_map[*it] == 0) continue; int len = 1; num_map[*it] = 0; int low = *it - 1; int high = *it + 1; bool bFindLow = true; bool bFindHigh = true; while (true) { bool bFind = false; unordered_map<int, int>::iterator iterMap; iterMap = num_map.find(low); if (bFindLow && iterMap != num_map.end() && iterMap->second) { ++len; --low; iterMap->second = 0; bFind = true; } else bFindLow = false; iterMap = num_map.find(high); if (bFindHigh && iterMap != num_map.end() && iterMap->second) { ++len; ++high; iterMap->second = 0; bFind = true; } else bFindHigh = false; if (!bFind) break; } if (len > maxLen) maxLen = len; } return maxLen; }
相关文章推荐
- LeetCode: Longest Consecutive Sequence [128]
- leetcode128 Longest Consecutive Sequence (map)
- leetcode之longest-consecutive-sequence
- 【LeetCode with Python】 Longest Consecutive Sequence
- [leetcode] 128. Longest Consecutive Sequence
- [LeetCode] Longest Consecutive Sequence
- LeetCode:Longest Consecutive Sequence
- leetcode 最长连续序列 longest consecutive sequence
- Leetcode: Longest Consecutive Sequence
- LeetCode -- Longest Consecutive Sequence
- [LeetCode] Longest Consecutive Sequence
- [leetcode] Longest Consecutive Sequence
- [leetcode]Longest Consecutive Sequence
- LeetCode[Map]: Longest Consecutive Sequence
- [LeetCode] Longest Consecutive Sequence
- leetcode - Longest Consecutive Sequence
- Longest Consecutive Sequence [LeetCode]
- leetcode 之Longest Consecutive Sequence(六)
- LeetCode-Longest Consecutive Sequence
- LeetCode: Longest Consecutive Sequence