leetcode+ 最长连续子序列,哈希

点击打开链接
class Solution {
public:
    int longestConsecutive(vector& nums) {
        int res = 0;
        unordered_map tr_left, tr_right;
        for(auto& x:nums){
            int left = tr_right[x-1];
            int right = tr_left[x+1];
            tr_left[x-left] = max(tr_left[x-left], left+1+right);
            tr_right[x+right] = max(tr_right[x+right], left+1+right);
            res = max(res, left+1+right);
        }
        return res;
    }
};

你可能感兴趣的:(Leetcode)