Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit

给一个数组, 和一个数, 求数组的子数组最大的长度,使其中的元素的绝对值不大于这个数.

简单的双指针遍历, 注意一下边界.

class Solution {
public:
    int longestSubarray(vector<int>& nums, int limit) {
        multiset <int, greater <int> > ms;       
        int res = 1;
        int left = 0;
        int right = 0;
        while(right < nums.size()){
            ms.insert(nums[right++]);
             while(abs(*ms.begin() - *ms.rbegin()) > limit){
                    ms.erase(ms.find(nums[left++])); 
            }
            res = max(res, right - left);
        }
        return res;
    }
};