連續數組和為S
和为S的连续正数序列_牛客网
https://www.nowcoder.com/practice/c451a3fd84b64cb19485dad758a55ebe?tpId=13&tqId=11194&tPage=3&rp=2&ru=%2Fta%2Fcoding-interviews&qru=%2Fta%2Fcoding-interviews%2Fquestion-ranking
# -*- coding:utf-8 -*- class Solution: def FindContinuousSequence(self, tsum): # write code here res = [] low,high = 1,2 while low<high: cursum = (low+high)*(high-low+1)/2 if cursum == tsum: tmp =[] for i in range(low,high+1): tmp.append(i) res.append(tmp) low += 1 #在當前連續數組計算完以後,連續數組的low,最小值要繼續+1,以便尋找下一個和為s的連續數組 elif cursum<tsum: high+= 1 else: low += 1 return res窮舉法:運用等差數列求和公式,左右指針,如果當前數列的和正好等於所給的數字,那麼append,加入進去以後,low指針要加1 ,以便尋找下一組和為S的連續數組,如果當前和小於所給數字,high+1,若大於,low+1。