Article Directory
Description of the problem
1- brute force solution to a problem
class Solution:
def findContinuousSequence(self, target: int) -> List[List[int]]:
result=[]
example=[]
for i in range(1,int(target/2)+1):
sum=0
j=i
while sum<target:
sum=sum+j
j=j+1
if sum==target:
example=list(range(i,j))
result.append(example)
return result
题解 2-root finding official 1
class Solution:
def findContinuousSequence(self, target: int) -> List[List[int]]:
res=[]
for start in range(1,int(target/2)+1):
delta=1 - 4 * (start - start * start - 2 * target)
if delta<0:
continue
elif (delta**0.5)*(delta**0.5)==delta and (-1+delta**0.5)%2==0:
end=int((-1+delta**0.5)/2)
res.append(list(range(start,end+1)))
return res
Roots math problem solution 3- way 2
class Solution:
def findContinuousSequence(self, target: int) -> List[List[int]]:
res=[]
for n in range(2,int((2*target)**0.5)+1):#这里从1开始加到n等于target
temp=target-n*(n-1)/2
if temp <= 0:
break
if not temp % n:
start=int(temp/n)
res.append([start+i for i in range(n)])
return res[::-1]#n多的数第一个数字小
Here time complexity is O (target ** 0.5)
Four pairs of pointers explanations
class Solution:
def findContinuousSequence(self, target: int) -> List[List[int]]:
res=[]
start=1
end=1
sum=0
while start<int(target/2)+1:
if sum<target:#滑动窗口左移
sum+=end
end+=1
elif sum>target:#滑动窗口右移
sum-=start
start+=1
else:
res.append(list(range(start,end)))
sum -= start
start += 1
return res