当前位置:网站首页>The sword refers to Offer 57 - II. and is a continuous positive sequence of s (sliding window)

The sword refers to Offer 57 - II. and is a continuous positive sequence of s (sliding window)

2022-08-09 13:47:00 A pig to

剑指 Offer 57 - II. 和为s的连续正数序列(滑动窗口)

class Solution {
    
public:
    vector<vector<int>> findContinuousSequence(int target) {
    
        vector<vector<int>>vec;
        vector<int> res;
        for (int l = 1, r = 2; l < r;){
    
            int sum = (l + r) * (r - l + 1) / 2;
            if (sum == target) {
    
                res.clear();
                for (int i = l; i <= r; ++i) {
    
                    res.emplace_back(i);
                }
                vec.emplace_back(res);
                l++;
            } else if (sum < target) {
    
                r++;
            } else {
    
                l++;
            }
        }
        return vec;
    }
};
原网站

版权声明
本文为[A pig to]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208091244510509.html