码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode – Refresh – Longest Consecutive Sequence

时间:2015-03-20 08:02:46      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

It use the hashset to do the tricks.

 1 class Solution {
 2 public:
 3     int longestConsecutive(vector<int> &num) {
 4         int len = num.size(), result = 0;
 5         if (len < 2) return len;
 6         unordered_set<int> sets;
 7         for (int i = 0; i < len; i++) {
 8             sets.insert(num[i]);
 9         }
10         while (!sets.empty()) {
11             int start = *sets.begin(), end = start;
12             while (sets.erase(start-1)) start--;
13             while (sets.erase(end)) end++;
14             result = max(result, end-start);
15         }
16         return result;
17     }
18 };

 

LeetCode – Refresh – Longest Consecutive Sequence

标签:

原文地址:http://www.cnblogs.com/shuashuashua/p/4352681.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!