首页 > 其他 > 详细

leetcode128 最长连续序列

时间:2021-04-25 14:02:22      阅读:16      评论:0      收藏:0      [点我收藏+]

思路:

使用哈希表。

实现:

 1 class Solution
 2 {
 3 public:
 4     int longestConsecutive(vector<int>& nums)
 5     {
 6         int n = nums.size();
 7         unordered_set<int> st{nums.begin(), nums.end()};
 8         int res = 0;
 9         for (auto it: st)
10         {
11             if (st.count(it - 1)) continue;
12             int num = it;
13             while (st.count(num)) num++;
14             res = max(res, num - it);
15         }
16         return res;
17     }
18 };

leetcode128 最长连续序列

原文:https://www.cnblogs.com/wangyiming/p/14699662.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!