num.sort() result =  for i in range(len(num)-2): if i > 0 and num[i]==num[i-1]: pass j = i + 1 k = len(num)-1 while j < k: sum = num[j] + num[k] + num[i] if sum == 0: result.append([num[i],num[j],num[k]]) j += 1 k -= 1 # check duplicate while j < k and num[j]==num[j-1]: j += 1 while j < k and num[k]==num[k+1]: k -= 1 elif sum < 0: j += 1 else: k -= 1 return result
Try this case: [-1, -1, -1, 2]
I got this problem just now.
Just change the
for i in range(len(num)-2) into while-loop.
It seems that the 'i' cannot be changed inside the for-loop.
hi, thank you so much for your help @illuz. In fact, I corrected this problem by using 'continue 'instead of 'pass' in the for loop. Thanks for your help anyway~
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.