当前位置:网站首页>剑指 Offer 57 - II. 和为s的连续正数序列(滑动窗口)

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

2022-08-09 12:45:00 养猪去

剑指 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;
    }
};
原网站

版权声明
本文为[养猪去]所创,转载请带上原文链接,感谢
https://song-yang-ji.blog.csdn.net/article/details/121659776