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

和为S的连续正数序列

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

# -*- coding:utf-8 -*-
class Solution:
    def FindContinuousSequence(self, tsum):
        # write code here
        
        ret = []
        if tsum == 1:
            return []
        leftCursor = 1
        rightCursort = 2
        curSum = 3
        
        if tsum == 3:
            return [[1, 2]]
        
        
        for idx, item in enumerate(range(3, 100+1)):
            curSum = curSum + item
#             print(curSum)
            if curSum == tsum and item > leftCursor:
                retItem = [i for i in range(leftCursor, item+1)]
                ret.append(retItem)
            elif curSum > tsum:
                while curSum > tsum:
                    curSum -= leftCursor
                    leftCursor += 1
                    if curSum == tsum and item > leftCursor :
                        retItem = [i for i in range(leftCursor, item+1)]
                        ret.append(retItem)               
                
                
        return ret
                
                
        
        
        
全部评论

相关推荐

07-03 16:02
门头沟学院 Java
点赞 评论 收藏
分享
找到实习了 给了150一天 但是说是低代码 值得去吗
码农索隆:是在没实习,可去,待个一两周,不行就润呗
点赞 评论 收藏
分享
Rena1ssanc...:对的,要是面评没太烂,勤更新简历等捞就行了,腾讯可以无限复活
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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