您当前的位置: 首页 >  leetcode

LeetCode Algorithm 剑指 Offer 57 - II. 和为s的连续正数序列

发布时间:2022-01-01 14:54:55 ,浏览量:0

剑指 Offer 57 - II. 和为s的连续正数序列

Ideas

区间问题首先想到用双指针。

因为这题没有给定数组,其实相当于就是一个从1到target的数组,然后直接套双指针的模板就可以了。

双指针教程参考:https://www.yuque.com/huoxiangshouxiangwanghuo/ndi0dn/moq12q

Code Python
from copy import deepcopy from typing import List class Solution: def findContinuousSequence(self, target: int) -> List[List[int]]: left, right = 1, 1 ans, res, sums = [], [], 0 while right < target: sums += right while sums > target: sums -= left
                res.pop(0) left += 1 res.append(right) right += 1 if sums == target: # 如果找到一个符合条件的区间 ans.append(deepcopy(res)) return ans
关注
打赏
1688896170
查看更多评论

暂无认证

  • 0浏览

    0关注

    108697博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0470s