题解 | #牛牛的三元组问题#
牛牛的三元组问题
https://www.nowcoder.com/practice/72c6d735fb1144a2ba162976a4510839
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param nums int整型一维数组
# @return int整型二维数组
#
class Solution:
def findTriplets(self , nums: List[int]):
# write code here
l = []
sortnums = sorted(nums)
for i in range(len(sortnums)-2):
j = i + 1
k = len(nums) - 1
while j < k:
if sortnums[i] + sortnums[j] + sortnums[k] > 0:
k -= 1
elif sortnums[i] + sortnums[j] + sortnums[k] < 0:
j += 1
else:
three_tuple = [sortnums[i],sortnums[j],sortnums[k]]
if len(l) == 0:
l.append(three_tuple)
else:
if l[-1] == three_tuple:
pass
else:
l.append(three_tuple)
k -= 1
j += 1
return l
基恩士成长空间 446人发布
