0
点赞
收藏
分享

微信扫一扫

LeetCode---128. 最长连续序列(如果没有比当前数字小1的数字时候再进行计数))


示例代码

class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        unordered_set<int> mySet;
        for(int num:nums){
            mySet.insert(num);
        }
        int res=0,count=1,temp=0;
        for(auto num:mySet){
            temp=num;
            count=1;
            //如果没有比当前数字小1的数字时候再进行计数
            if(!mySet.count(num-1)){
                while(mySet.count(++temp)){
                    count++;
                }
            }
            //如果此序列计数的值比结果大,则进行结果值的更新
            if(count>res){
                res=count;
            }
        }
        return res;
    }
};

效果展示

LeetCode---128. 最长连续序列(如果没有比当前数字小1的数字时候再进行计数))_算法


举报

相关推荐

0 条评论