leetCode-Record icon indicating copy to clipboard operation
leetCode-Record copied to clipboard

面试题57 - II. 和为s的连续正数序列

Open fireairforce opened this issue 5 years ago • 0 comments

这题我是用二分写的 = =QAQ:

/**
 * @param {number} target
 * @return {number[][]}
 */
var findContinuousSequence = function(target) {
     let low = 1,
        high = 2,
        array = [];
    while (high > low) {
        let cur = (high + low) * (high - low + 1) / 2;
        if (cur < target) {
            high++;
        }
        if (cur === target) {
            let arr = [];
            for (let i = low; i <= high; i++) {
                arr.push(i);
            }
            array.push(arr);
            low++;
        }
        if (cur > target) {
            low++;
        }
    }
    return array;
};

fireairforce avatar Mar 06 '20 15:03 fireairforce