求Leetcode 3Sum 能过大数据的python解法……# JobHunting - 待字闺中
h*7
1 楼
不光是这道题,还有一些别的题python过不了。不才解法如下,之前写的一个不用查
list的版本也没过。
另外貌似像import sys; min = sys.maxint 这种都编译不过去?
class Solution:
# @return a list of lists of length 3, [[val1,val2,val3]]
def threeSum(self, num):
num.sort()
n = len(num)
res= []
if n<3:
return res
for i in range(n-2):
k = i + 1
j = n - 1
while k < j:
sum = num[i] + num[j] + num[k]
if sum < 0:
k += 1
elif sum > 0:
j -= 1
else:
comb = [num[i], num[k], num[j]]
if comb not in res:
res.append(comb)
k += 1
j -= 1
return res
list的版本也没过。
另外貌似像import sys; min = sys.maxint 这种都编译不过去?
class Solution:
# @return a list of lists of length 3, [[val1,val2,val3]]
def threeSum(self, num):
num.sort()
n = len(num)
res= []
if n<3:
return res
for i in range(n-2):
k = i + 1
j = n - 1
while k < j:
sum = num[i] + num[j] + num[k]
if sum < 0:
k += 1
elif sum > 0:
j -= 1
else:
comb = [num[i], num[k], num[j]]
if comb not in res:
res.append(comb)
k += 1
j -= 1
return res