☆☆☆https://leetcode.com/problems/3sum/

来源:互联网 发布:运营商网络制式 编辑:程序博客网 时间:2024/06/05 08:36

https://leetcode.com/problems/3sum/

这道题由于之前写过two sum 可以先固定一个 另外两个用类似twosum的方法 

但是niddle就不需要了 因为这道题允许有多个解

我没有写另一个函数 直接把代码都写在一起 不过效率很低 

class Solution:    # @return a list of lists of length 3, [[val1,val2,val3]]    def threeSum(self, num):        solution=[]        num.sort()        length=len(num)        for i in range(length-2):            for j in range(i+1,length-1):                x=0-num[i]-num[j]                if x in num[j+1:]:                    templist=[num[i],num[j],x]                    #templist.sort()                    if templist not in solution:                        solution.append(templist)        return solution

一会儿再看看有没有别的方法 

0 0
原创粉丝点击