leetcode-面57 和为s的连续正数序列
2020-03-15 12:14
447 查看
方法一:数学方法
class Solution: def findContinuousSequence(self, target: int) -> List[List[int]]: res = [] for n in range(2, target - 1): temp = target - n*(n-1)//2 if temp <= 0: break if not temp % n: a1 = temp // n res.append([a1 + i for i in range(n)]) return res[::-1]``` 方法二:滑动窗口法 ```python class Solution: def findContinuousSequence(self, target: int) -> List[List[int]]: i, j, sum = 1, 1, 0 res = [] while i <= target // 2: if sum < target: sum += j j += 1 elif sum > target: sum -= i i += 1 else: t = list(range(i,j)) res.append(t) sum -= i i += 1 return res
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- 力扣LeetCode【每日一题】—— 面试题解答57 - II. 和为s的连续正数序列(Python3)滑动窗口
- 【Golang】LeetCode-剑指Offer-面试题57 - II - 和为s的连续正数序列【两种解法】
- 面试题57. 和为s的两个数字 / 面试题57 - II. 和为s的连续正数序列
- 剑指Offer(57)和为S的连续正数序列
- 面试题57 - II. 和为s的连续正数序列
- 57、输出和为S的连续正数序列
- 【剑指offer】面试题57(2):和为S的连续正数序列
- 面试题57-题目二:和为S的连续正数序列
- 力扣面试题57 - II. 和为s的连续正数序列 详解滑动窗口法 python3实现
- 和为S的连续正数序列
- 剑指Offer面试题41和为s的两个数字与和为s的连续正数序列,面试题42翻转单词顺序与左旋转字符串
- 和为S的连续正数序列
- 剑指offer-和为s的两个数字VS和为s的连续正数序列
- 面试题41:和为s的两个数字VS和为s的连续正数序列
- 剑指offer--和为S的连续正数序列
- 《剑指offer》- 输出所有和为S的连续正数序列
- 剑指Offer之和为S的连续正数序列
- 【LeetCode】Python实现-674. 最长连续递增序列
- 【剑指offer之和为s的两个数字VS和为s的连续正数序列】
- 和为n的连续正数序列