LeetCode Algorithm 剑指 Offer 57 - II. 和为s的连续正数序列
生活随笔
收集整理的這篇文章主要介紹了
LeetCode Algorithm 剑指 Offer 57 - II. 和为s的连续正数序列
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
劍指 Offer 57 - II. 和為s的連續(xù)正數(shù)序列
Ideas
區(qū)間問題首先想到用雙指針。
因?yàn)檫@題沒有給定數(shù)組,其實(shí)相當(dāng)于就是一個(gè)從1到target的數(shù)組,然后直接套雙指針的模板就可以了。
雙指針教程參考: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: # 如果找到一個(gè)符合條件的區(qū)間ans.append(deepcopy(res))return ans總結(jié)
以上是生活随笔為你收集整理的LeetCode Algorithm 剑指 Offer 57 - II. 和为s的连续正数序列的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2020年第十一届蓝桥杯 - 省赛 -
- 下一篇: LeetCode Algorithm 1