LeetCode Algorithm 剑指 Offer 57 - II. 和为s的连续正数序列
生活随笔
收集整理的這篇文章主要介紹了
LeetCode Algorithm 剑指 Offer 57 - II. 和为s的连续正数序列
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
劍指 Offer 57 - II. 和為s的連續正數序列
Ideas
區間問題首先想到用雙指針。
因為這題沒有給定數組,其實相當于就是一個從1到target的數組,然后直接套雙指針的模板就可以了。
雙指針教程參考:https://www.yuque.com/huoxiangshouxiangwanghuo/ndi0dn/moq12q
Code
Python
from copy import deepcopy from typing import Listclass Solution:def findContinuousSequence(self, target: int) -> List[List[int]]:left, right = 1, 1ans, res, sums = [], [], 0while right < target:sums += rightwhile sums > target:sums -= leftres.pop(0)left += 1res.append(right)right += 1if sums == target: # 如果找到一個符合條件的區間ans.append(deepcopy(res))return ans總結
以上是生活随笔為你收集整理的LeetCode Algorithm 剑指 Offer 57 - II. 和为s的连续正数序列的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2020年第十一届蓝桥杯 - 省赛 -
- 下一篇: ☆ 10个小技巧,让你的 Python