Python in O(N^2)


  • 1
    A
    def threeSumSmaller(self, nums, target):
        nums.sort()
        ans = 0
        size = len(nums)
        for i in xrange(size-2):
            start = i + 1
            end = size - 1
            now = nums[i]
            while start < end:
                if now + nums[start] + nums[end] < target:
                    ans += (end - start)
                    start += 1
                else:
                    end -= 1
        return ans

Log in to reply
 

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