Concise python two pointer solution O(N^2)


  • 0
    L
    class Solution(object):
    def threeSumSmaller(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        nums.sort()
        count = 0
        for i in xrange(len(nums)):
            start,end = i+1,len(nums)-1
            while(start < end):
                if nums[start] + nums[end] < target - nums[i]:
                    count = count + (end-start)
                    start = start + 1
                else:
                    end = end - 1
        return count

Log in to reply
 

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