LeetCode 128 Longest Consecutive Sequence

2018-11-22 10:38:28 浏览数 (1)

题目LeetCode 128

水题一枚

代码语言:javascript复制
class Solution {
public:
    int tag[100005];
    int longestConsecutive(vector<int>& nums) {
        
        sort(nums.begin(), nums.end(),less<int>());
        int ans = 0 ;
        if(nums.size()>0)
            ans =1;
        memset(tag,1,sizeof(tag));
        tag[0]=1;
        for(int i=1;i<nums.size();i  )
        {
             if(nums[i]==nums[i-1] 1)
                 tag[i]=tag[i-1] 1;
            else if(nums[i]==nums[i-1])
                 tag[i] = tag[i-1];
            else
                tag[i]=1;
            ans = max(ans,tag[i]);
        }
        return ans;
    }
};

0 人点赞