小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100。但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数)。没多久,他就得到另一组连续正数和为100的序列:18,19,20,21,22。现在把问题交给你,你能不能也很快的找出所有和为S的连续正数序列?
数据范围:
进阶:时间复杂度
进阶:时间复杂度
输出所有和为S的连续正数序列。序列内按照从小至大的顺序,序列间按照开始数字从小到大的顺序
9
[[2,3,4],[4,5]]
0
[]
class Solution: def FindContinuousSequence(self, tsum): # write code here i = 0 if tsum <=2: return None res1 = [] for i in range(1,tsum): n = 2 sum = i while sum<tsum: sum = sum + i+n-1 n = n+1 if sum == tsum: res1.append([x for x in range(i,i+n-1)]) if sum >= tsum: break return res1 滑动窗口
# -*- coding:utf-8 -*- # 思路比较简单,首先根据输入的数值和可以确定连续的数字的基本范围array # 假设和为tsum的连续正整数为n个,则它们的平均值为tsum/n,那么这连续的几个正整数肯定在平均值左右, # 这时只需要在平均值附件左右找n个数,如果和为tsum,则就是其中一个解... class Solution: def FindContinuousSequence(self, tsum): # write code here if tsum <= 2: return [] if tsum % 2: array = [i for i in range(1, tsum / 2 + 2)] else: array = [i for i in range(1, tsum / 2 + 1)] res = [] for index in range(len(array), 1, -1): mean_value = tsum / index if tsum % index: temp = [mean_value, mean_value + 1] else: temp = [mean_value - 1, mean_value, mean_value + 1] while len(temp) != index: if temp[0] == 1 or temp[-1] == len(array): break temp = [temp[0] - 1] + temp + [temp[-1] + 1] if sum(temp) == tsum: res.append(temp) return res
# -*- coding:utf-8 -*- class Solution: def FindContinuousSequence(self, tsum): # write code here import math lis=[i for i in range(tsum)] ans=[] for n in range(int(math.sqrt(2*tsum)),1,-1): if n % 2 == 0 and (tsum % n)*2 == n: ans.append(lis[tsum/n-n/2+1:tsum/n+n/2+1]) if n % 2 == 1 and tsum % n == 0: ans.append(lis[tsum/n-n/2:tsum/n+n/2+1]) return ans
# -*- coding:utf-8 -*- class Solution: def FindContinuousSequence(self, tsum): # write code here if tsum<=0: return [] i=1 j=2 res=[] while i<=tsum/2: l=[] for k in range(i,j+1): l.append(k) sum1=sum(l) if sum1==tsum: res.append(l) i=i+1 j=i+1 elif sum1<tsum: j+=1 elif sum1>tsum: i+=1 return res
# -*- coding:utf-8 -*- class Solution: def FindContinuousSequence(self, tsum): # write code here if tsum<3: return [] a = list(range(1,tsum)) output = [] for i in range(len(a)-1): for j in range(i+1,len(a)): temp = sum(a[i:j+1]) if temp==tsum: output.append(a[i:j+1]) elif temp>tsum: break return output
# -*- coding:utf-8 -*- class Solution: def FindContinuousSequence(self, tsum): # write code here result = [] for i in range(1,tsum): s = i + 1 cum = 0 while cum < tsum and s <= 100: cum = (i+s)*(s-i+1)/2 if cum == tsum: result.append(list(range(i,s+1))) s += 1 return result
# -*- coding:utf-8 -*- class Solution: def FindContinuousSequence(self, tsum): # write code here s = [1, 2] self.results = [] while s[0] < tsum//2+1: if sum(s) < tsum: s.append(s[-1]+1) elif sum(s) > tsum: s.pop(0) else: self.results.append(s[:]) # deep copy s.pop(0) return self.results
# -*- coding:utf-8 -*- class Solution: def FindContinuousSequence(self, tsum): # write code here ans=[] if tsum<=2: return ans for i in range(2,tsum): q=i*(i-1)/2 if q<tsum: if (tsum-q)%i==0:#保证a1是整数 a1=(tsum-q)/i #i越大,list的开头的值就越小, #所以每次用insert(0,obj),可以保证长的在前面 ans.insert(0,range(a1,a1+i)) else: break return ans
# -*- coding:utf-8 -*- class Solution: def FindContinuousSequence(self, n): # write code here def dfs(n,first,last,res): if sum(res) >= n: if sum(res) == n: arr.append([i for i in res]) return for i in range(first,last): res.append(i) dfs(n,i+1,i+2,res) res.pop() arr = [] dfs(n,1,n,[]) return arr大家可以试一试通过深度遍历,一样可以ac
class Solution: def FindContinuousSequence(self, tsum): # write code here sm = 3 left = 1 right = 2 result = [] while left < right and right < tsum // 2 + 2: if sm == tsum: result.append([i for i in range(left, right + 1)]) sm -= left left += 1 elif sm < tsum: right += 1 sm = sm+right else: sm -= left left += 1 return result
class Solution: def FindContinuousSequence(self,tsum): total=[] for i in range(tsum//2+1,1,-1): if i%2==0: if (tsum/i)%1==0.5 and int(tsum/i-0.5*(i-1))>0: result=[int(tsum/i-0.5*(2*j+1)) for j in range(i//2)]+[int(tsum/i+0.5*(2*j+1)) for j in range(i//2)] total.append(sorted(result)) if i%2==1: if (tsum/i)%1==0 and int(tsum/i-i//2)>0: result=[int(tsum//i)]+[int(tsum/i-j) for j in range(1,i//2+1)]+[int(tsum/i+j) for j in range(1,i//2+1)] total.append(sorted(result)) return(total)
# -*- coding:utf-8 -*- class Solution: def FindContinuousSequence(self, tsum): # write code here res = [] #i为正数序列起始值,j为序列正数个数 for i in range(1,tsum//2+1): for j in range(2,tsum): if (i+i+j-1)/2.0*j == tsum: res.append([a for a in range(i,i+j)]) return res
# -*- coding:utf-8 -*- class Solution: def FindContinuousSequence(self, tsum): # write code here #先考虑特殊情况 if tsum < 3: return [] array = [] for i in range(1, tsum): t = 0 j = i while t < tsum : t = t+j j += 1 if t == tsum: array.append(range(i,j)) return array
class Solution: def FindContinuousSequence(self, tsum): s=[] for i in range(1,tsum//2+1): b = pow( (2*tsum + pow(i-0.5, 2)), 0.5) - 0.5 if b == int(b) and b > i: s.append(range(i,int(b+1))) return s假设序列的最小值为a,最大值为b,b > a。那么序列求和为(a+b)(b-a+1)/2 = S,即(b+0.5)2-(a-0.5)2=2S,因此b = (2S+(a-0.5)2)0.5-0.5,如果b是整数且大于a,那么b为一个解。
# -*- coding:utf-8 -*- class Solution: def FindContinuousSequence(self, tsum): # write code here ## 利用双指针的方法,a表示连续序列的开始,n表示连续序列的个数,total连续序列,利用的一个思想就是 ## 当a越小n所需要的值越大。随着a不断增大n不断的减小。a的最小值为1,n的最大值为tsum-1 a, n, total = 1, tsum-1, [] index = 0 while(a<tsum and n>0): ## 根据连续序列的开始和连续序列的个数求连续序列的和 num = (2*a+n-1)*n/2 if int(num)==tsum: index += 1 total.append([i for i in range(a, a+n)]) # 当和大于当前值,就是连续个数太多了,n进去1 if num>tsum: n -= 1 # 当和小于当前值,就是序列开始的值太小,需要往右走。 else: a += 1 if len(total)==0: return [] else: return total
# -*- coding:utf-8 -*- class Solution: def FindContinuousSequence(self, tsum): if tsum <= 2: return list() head = 1 tail = 2 res = list() while head < tail: tmp = tail * tail - head * head + head + tail if tmp == 2 * tsum: res.append(list(range(head, tail + 1))) head += 1 elif tmp < 2 * tsum: tail += 1 else: head += 1 return res
# -*- coding:utf-8 -*- class Solution: def FindContinuousSequence(self, tsum): # write code here list1=[] for i in range(1, tsum): sum1 = i for j in range(i+1, tsum): sum1 += j if sum1 == tsum: list1.append([m for m in range(i, j+1)]) elif sum1 > tsum: break return list1
# -*- coding:utf-8 -*- class Solution: def FindContinuousSequence(self, tsum): l = list(range(1, tsum+1)) i = 0 j = 1 ret = [] while i<tsum: tmp = sum(l[i:j]) if tmp == tsum: # 若等于 if i+1==j: # 单个数字不算 pass else: ret.append(l[i:j]) i+=1 elif tmp > tsum: #若大于前后缩减 i+=1 j-=1 else: #若小于继续加 j += 1 return ret