ソートされた配列に数値があるかどうかを調べる関数があります。s
は、合計であるx
になります。私はこの機能のために大規模な複雑さが何であるかを知りたい。私はそれがO(n)で動くと思うが、私は確信していない。私の機能はO(n)で動作しますか?
機能:
def sumInside(s, x):
# Two indices that will be compared
l = 0
r = len(s) - 1
# Go through the array for the elements
while l < r:
if s[l] + s[r] == x:
return True
elif s[l] + s[r] < x:
l += 1
else:
r -= 1
return False
ありがとうございました!ルールを念頭に置いておきます – tushariyer