class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> ls = new LinkedList<>();
 
        for (int i = 0; i < nums.length - 2; i++) {
            if (i == 0 || (i > 0 && nums[i] != nums[i - 1])) {  // 跳过可能重复的答案
 
                int l = i + 1, r = nums.length - 1, sum = 0 - nums[i];
                while (l < r) {
                    if (nums[l] + nums[r] == sum) {
                        ls.add(Arrays.asList(nums[i], nums[l], nums[r]));
                        while (l < r && nums[l] == nums[l + 1]) l++;
                        while (l < r && nums[r] == nums[r - 1]) r--;
                        l++;
                        r--;
                    } else if (nums[l] + nums[r] < sum) {
                        while (l < r && nums[l] == nums[l + 1]) l++;   // 跳过重复值
                        l++;
                    } else {
                        while (l < r && nums[r] == nums[r - 1]) r--;
                        r--;
                    }
                }
            }
        }
        return ls;
    }
}

 

使用python实现:

 1 class Solution:
 2     def threeSum(self, nums: 'List[int]') -> 'List[List[int]]':
 3         nums = sorted(nums)
 4         res = []
 5         n = len(nums)
 6         for i in range(n-2):
 7             if i == 0 or nums[i] != nums[i-1]:
 8                 l,r,sums = i + 1,n-1,0-nums[i]
 9                 while l < r:
10                     if nums[l] + nums[r] == sums:
11                         res.append([nums[i],nums[l],nums[r]])
12                         while l < r and nums[l] == nums[l+1]:
13                             l += 1
14                         while l < r and nums[r] == nums[r-1]:
15                             r -= 1
16                         l += 1
17                         r -= 1
18                     elif nums[l] + nums[r] < sums:
19                         while l < r and nums[l] == nums[l+1]:
20                             l += 1
21                         l += 1
22                     else:
23                         while l < r and nums[r] == nums[r-1]:
24                             r -= 1
25                         r -= 1
26         return res

 

下面补充另一种思路,使用python实现,是根据leetcode167 twonum II的思路来做的,效率比上面的方法要高一些,代码如下:

 1 class Solution:
 2     def towSum(self,numbers,target):
 3         i = 0
 4         j = len(numbers) - 1
 5 
 6         ary = list()
 7         while i < j:
 8             sums = numbers[i] + numbers[j]
 9             #print(str(i)+','+str(j)+'->'+str(sums))
10             if sums == target:
11                 #return numbers[i],numbers[j]
12                 ary.append([numbers[i],numbers[j]])
13                 while i < j and numbers[i]==numbers[i+1]:
14                     i+=1
15                 i+=1
16             elif sums < target:
17                 i+=1
18             else:
19                 j-=1
20         return ary
21 
22     def threeSum(self, nums: 'List[int]') -> 'List[List[int]]':
23         result = list()
24         
25         sortedlist = sorted(nums)
26 
27         zlist = list(filter(lambda x:x==0,sortedlist))
28         if len(zlist) >= 3:
29             result.append([0,0,0])
30 
31         nlist = list(filter(lambda x:x<0,sortedlist))
32         nset = set(nlist)
33 
34         plist = list(filter(lambda x:x>=0,sortedlist))
35         pset = set(plist)
36         for target in nset:
37             aa = self.towSum(plist,target*-1)
38             for a in aa:
39                 a1 = a[0]
40                 a2 = a[1]
41                 clist = [a1,a2,target]
42                 result.append(clist)
43                     
44 
45         for target in pset:
46             aa = self.towSum(nlist,target*-1)
47             for a in aa:
48                 a1 = a[0]
49                 a2 = a[1]
50                 clist = [a1,a2,target]
51                 result.append(clist)
52         
53         return result

 

posted on 2018-10-15 14:55  Sempron2800+  阅读(224)  评论(0编辑  收藏  举报