和为s的连续正数序列-2

力扣地址

双指针

class Solution {
public:

    void insertVec(vector<vector<int>>& ret, int low, int high) {
        vector<int> tmp;
        tmp.reserve(high - low + 1);
        for (int i = low; i <= high; ++i) {
            tmp.push_back(i);
        }
        ret.push_back(tmp);
    }

    vector<vector<int>> findContinuousSequence(int target) {
        int left = 1;
        int res = 0;
        vector<vector<int>> ret;
        for (int right = 1; right <= (target >> 1) + 1; ++right) {
            res += right;
            while (res > target) {
                res -= left++;
            }
            if (res == target) {
                insertVec(ret, left, right);
            }
        }
        return ret;
    }
};

猜你喜欢

转载自blog.csdn.net/TESE_yan/article/details/114230927