15. 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: 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]
]

java

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> list = new ArrayList<>();
        if (nums == null || nums.length < 3) {
            return list;
        }   
        Arrays.sort(nums);
        for (int i = 0; i < nums.length - 2; i++) {
            if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }
            int left = i + 1;
            int target = -nums[i];
            twoSum(list, nums, left, target);
        }
        return list;
    }
    private void twoSum(List<List<Integer>> list, int[] nums, int left, int target) {
        int right = nums.length - 1;
        int value = 0;
        List<Integer> arr = new ArrayList<>();
        while (left < right) {
            value = nums[left] + nums[right];
            if (left < right && value > target) {
                right--;
            } 
            if (left < right && value < target) {
                left++;
            }
            if (left < right && value == target) {
                arr.add(-target);
                arr.add(nums[left]);
                arr.add(nums[right]);
                list.add(new ArrayList<Integer>(arr));
                arr.clear();
                left++;
                right--;
                while (left < right && nums[left] == nums[left - 1]) {
                    left++;
                }
                while (left < right && nums[right] == nums[right + 1]) {
                    right--;
                }
            }
        }
    }
}


python
class Solution(object):
    def threeSum(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        if nums == None or len(nums) < 3:
            return []
        res = []
        nums.sort()
        for i in range(0, len(nums) - 2):
            if i > 0 and nums[i] == nums[i - 1]:
                continue
            target, left = -nums[i], i + 1
            self.twoSum(res, left, nums, target)
        return res
            
    def twoSum(self, res, start, nums, target):
        left, right = start, len(nums) - 1
        while left < right:
            value = nums[left] + nums[right]
            if left < right and value < target:
                left += 1
            if left < right and value > target:
                right -= 1
            if left < right and value == target:
                res.append([-target, nums[left], nums[right]])
                left, right = left + 1, right - 1
                while left < right and nums[left] == nums[left - 1]:
                    left += 1
                while left < right and nums[right] == nums[right + 1]:
                    right -= 1
        

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ncst

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值