美文网首页
57.和为s的连续正数序列

57.和为s的连续正数序列

作者: 寂灭天骄小童鞋 | 来源:发表于2020-03-06 17:56 被阅读0次

    https://leetcode-cn.com/problems/he-wei-sde-lian-xu-zheng-shu-xu-lie-lcof/

    func findContinuousSequence(_ target: Int) -> [[Int]] {
        var result = [[Int]]()
        var l = 1
        var r = 2
        var sum = 0
        while l < r {
            sum = (l + r) * (r - l + 1) / 2
            if sum == target {
                result.append(createArr(l, r))
                l = l + 1
            } else if sum > target {
                l = l + 1
            } else if sum < target {
                r = r + 1
            }
        }
        return result
    }
    
    func createArr(_ start: Int, _ end: Int) -> [Int] {
        var result = [Int]()
        var startV = start
        
        while startV <= end {
            result.append(startV)
            startV = startV + 1
        }
        return result
    }
    
    

    相关文章

      网友评论

          本文标题:57.和为s的连续正数序列

          本文链接:https://www.haomeiwen.com/subject/ciyirhtx.html