博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
和为s的连续正数序列
阅读量:3982 次
发布时间:2019-05-24

本文共 692 字,大约阅读时间需要 2 分钟。

Q:输入一个正整数 target ,输出所有和为 target 的连续正整数序列(至少含有两个数)。

序列内的数字由小到大排列,不同序列按照首个数字从小到大排列。

示例 1:

输入:target = 9输出:[[2,3,4],[4,5]]
class Solution:    def findContinuousSequence(self, target: int) -> List[List[int]]:        res = []        # 从1开始,依次遍历到target,求和,与target判断大小,符合条件的元素放入临时数组中        for i in range(1,target):            ls = []            temp = i            ls.append(i)            for j in range(i+1,target):                temp += j                if temp < target:                    ls.append(j)                elif temp == target:                    ls.append(j)                    res.append(ls)                    break                else:                    break        return res

 

转载地址:http://mejui.baihongyu.com/

你可能感兴趣的文章
Go学习(8):指针
查看>>
Go学习(9):结构体
查看>>
Go学习(10):方法
查看>>
Go学习(11):接口
查看>>
Go学习(12):面向对象
查看>>
Go学习(13):异常
查看>>
Go学习(14):defer
查看>>
Go学习(15):并发与包
查看>>
Go学习(16):网络编程
查看>>
深入浅出区块链笔记
查看>>
Oracle学习(一)
查看>>
Oracle学习(二)
查看>>
[Spring Boot]Spring Boot Configuration Annotation Processor not found in classpath
查看>>
SpringBoot简单整合Swagger2
查看>>
websocket教程(一) 非常有趣的理解websocket
查看>>
spring事件机制
查看>>
Maven错误:was cached in the local repository, resolution will not be reattempted until the update
查看>>
Excel2013每次打开都提示重新安装/正在配置
查看>>
Oracle:ORA-00911: 无效字符
查看>>
Oracle:查询各组最新的一条记录
查看>>