我解决了a problem on LeetCode,我发现an answer时间复杂度为O(n ^ 2),为什么?这是代码
class Solution(object):
def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
result = []
nums.sort()
for i in xrange(len(nums)-2):
if i > 0 and nums[i] == nums[i-1]:
continue
l, r = i + 1, len(nums) - 1
while l < r:
if nums[l] + nums[r] == -nums[i]:
result.append([nums[i], nums[l], nums[r]])
l, r = l + 1, r - 1
while l < r and nums[l] == nums[l-1]:
l += 1
while l < r and nums[r] == nums[r+1]:
r -= 1
elif nums[l] + nums[r] > -nums[i]:
r -= 1
else:
l += 1
return result