3Sum and 4Sum @ Python Leetcode

 

1) 3Sum 

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

Given an array S of n integers, are there elements abc in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note:

  • Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
  • The solution set must not contain duplicate triplets.

 

    For example, given array S = {-1 0 1 2 -1 -4},

    A solution set is:
    (-1, 0, 1)
    (-1, -1, 2)

class Solution:
    # @return a list of lists of length 3, [[val1,val2,val3]]
    def threeSum(self, num):
        num.sort()
        ans = []
        target = 0
        for i in range(0, len(num)):
            if (i > 0 and num[i] == num[i-1]): continue  # only reserve first of all same values 
            l, r = i + 1, len(num) - 1
            while l < r:
                sum = num[i] + num[l] + num[r] 
                if sum == target:
                    ans.append([num[i], num[l], num[r]])
                    while l < r and num[l] == num[l + 1]: l = l + 1  # remove duplicate
                    while l < r and num[r] == num[r - 1]: r = r - 1  # remove duplicate
                    l, r = l + 1, r - 1
                elif sum < target:
                    l = l + 1
                else:
                    r = r - 1   
        return ans

 

 

2) 4Sum

https://oj.leetcode.com/problems/4sum/

Given an array S of n integers, are there elements abc, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.

Note:

  • Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
  • The solution set must not contain duplicate quadruplets.

 

    For example, given array S = {1 0 -1 0 -2 2}, and target = 0.

    A solution set is:
    (-1,  0, 0, 1)
    (-2, -1, 1, 2)
    (-2,  0, 0, 2)
class Solution:
    # @return a list of lists of length 4, [[val1,val2,val3,val4]]
    def fourSum(self, num, target):
        num.sort()
        ans = []
        for i in range(0, len(num)):
            if (i > 0 and num[i] == num[i-1]): continue  # only reserve first of all same values 
            for j in range(i+1, len(num)):
                if (j > i + 1 and num[j] == num[j-1]): continue  # only reserve first of all same values 
                l, r = j + 1, len(num) - 1
                while l < r:
                    sum = num[i] + num[j] + num[l] + num[r] 
                    if sum == target:
                        ans.append([num[i], num[j], num[l], num[r]])
                        while l < r and num[l] == num[l + 1]: l = l + 1  # remove duplicate
                        while l < r and num[r] == num[r - 1]: r = r - 1  # remove duplicate
                        l, r = l + 1, r - 1
                    elif sum < target:
                        l = l + 1
                    else:
                        r = r - 1   
        return ans

 

posted on 2014-09-12 10:18  AIDasr  阅读(235)  评论(0编辑  收藏  举报

导航