题解 | #和为S的连续正数序列#

和为S的连续正数序列

https://www.nowcoder.com/practice/c451a3fd84b64cb19485dad758a55ebe

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 * 
 * @param sum int整型 
 * @return int整型二维数组
 */
export function FindContinuousSequence(sum: number): number[][] {
    // write code here
    let start = 1
    let end = 1
    const res = []
    const halfValue = Math.ceil(sum / 2)
    let total = start
    while (start <= halfValue && end < sum) {
        if (total === sum) {
            const subValue = []
            let i = start
            while (i <= end) {
                subValue.push(i)
                i++
            }
            res.push(subValue)
            start++
            end = start
            total = start
        } else if (total < sum) {
            end++
            total += end
        } else if (total > sum) {
            start++
            end = start
            total = start
        }
    }
    return res
}

全部评论

相关推荐

07-09 19:25
门头沟学院 Java
这是要把每一个投校招的都开盒吗?
26届之耻将大局逆转:裁人的时候一次性追回餐费
点赞 评论 收藏
分享
哈哈哈哈哈哈哈哈哈哈这个世界太美好了
凉风落木楚山秋:毕业出路老师不管,你盖个章他好交差就完事了,等你盖完毕业了就不关他事情了
点赞 评论 收藏
分享
水墨不写bug:疑似没有上过大学
点赞 评论 收藏
分享
昨天 10:56
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务