TLE Python!!! Please help me...


  • 0
    V

    class Solution(object):
    fstack=[]
    def calculate(self,nums,n,pos,tstack):
    #print tstack
    if n==0:
    if sum(tstack)==0:
    self.fstack.append(tstack)
    return

        for i in xrange(pos, len(nums)):
            if n>1 and i==len(nums)-1:
                continue
            else:
                if sum(tstack)+nums[i]>0:
                    break
                else:
                    self.calculate(nums,n-1,i+1,tstack+[nums[i]])
            
    
    
    def threeSum(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        self.fstack=[]
        if len(nums)<3:
            return []
        self.calculate(sorted(nums),3,0,[])
        nstack=[]
        for i in self.fstack:
            if sorted(i) not in nstack:
                nstack.append(sorted(i))
        return nstack

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.