subject : Xiao Ming likes mathematics very much , One day when he was doing his math homework , Require calculation 9~16 And , He immediately wrote out the correct answer is 100. But he is not satisfied with it , He was thinking about how many consecutive positive sequences sum up to 100( Include at least two numbers ). It didn't take long , And he gets another set of consecutive positive numbers 100 Sequence :18,19,20,21,22. Now leave it to you , Can you also quickly find out all the sum for S Continuous positive sequence of ?
Ideas : Speed pointer . The qualification is , The sum of these sequences will not exceed s Half of ,s/2 + (s/2 +1) Must be greater than s, So the end condition of the cycle is : Less than s/2.
1) When the sum of this sequence <s when , Quick pointer +1;
2) When the sequence and <=s when (=s when , Record the sequence and ), Let the current sum minus the minimum value approach slowly s.
Code :
class Solution:
def func(self , s):
end = s // 2
lo , hi , cur_sum = 1 ,2 ,3
res = []
while lo < end:
if cur_sum < s:
hi += 1
cur_sum += hi
else:
if cur_sum == s:
res.append(list(range(lo , hi+1)))
cur_sum -= lo
lo += 1
Record common errors WARNINGS: