文章

99

粉丝

120

获赞

8

访问

75.1k

头像
最长连续序列 128
备考笔记
发布于2024年9月6日 16:07
阅读数 725

class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        unordered_set<int>se;
        for(int x: nums)se.insert(x);
        int ans = 0;
        for(int x : se){
            if(!se.count(x-1)){
                int cur = x, cc = 1;
                while(se.count(cur+1))cur++,cc++;
                ans = max(ans, cc);
            }
        }
        return ans;
    }
};

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发