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]
]
題意:給定一個數組,輸出數組中三個數加起來等於目標數的所有組合(所有組合中不會有重複的元素)


題解:

將數組進行排序,然後

        i 進行循序歷遍
        j 由i進行向右夾逼
        k 由數組最後一個進行向左夾逼

例如:
        進行左右指針夾逼的動作
                     i j     k
                    1 2 3 4 5
                => i   j k

進行數組匹配時有三種情況:
             1.若nums[i] + nums[j] + nums[k] == 0
                則加入該結果
                並且讓i與j指針更靠近(因為已經有答案了,故可以將指針向內)
        
             2.若nums[i] + nums[j] + nums[k] > 0
                移動右指針k
        
             3.若nums[i] + nums[j] + nums[k] < 0
                移動左指針j

注意避免重複元素加入答案當中

package LeetCode.Medium;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ThreeSum {
    public List<List<Integer>> threeSum(int[] nums) {
        //先將數組進行排序(以方便進行夾逼)
        Arrays.sort(nums);
        
        List<List<Integer>> result = new ArrayList<>();
        
        /*
        i 進行循序歷遍
        j 由i進行向右夾逼
        k 由數組最後一個進行向左夾逼
        
                若nums[i] + nums[j] + nums[k] == 0
                則加入該結果
                並且讓i與j指針更靠近(因為已經有答案了,故可以將指針向內)
        
                若nums[i] + nums[j] + nums[k] > 0
                移動右指針k
        
                若nums[i] + nums[j] + nums[k] < 0
                移動左指針j
        
        */
        
        for(int i = 0; i < nums.length; i ++) {
            //避免i重複的情形
            if(i == 0 || nums[i] > nums[i - 1]) {
                int j = i + 1;
                int k = nums.length - 1;
                
                //進行左右指針夾逼的動作
                /*     i j     k
                 * ex: 1 2 3 4 5
                 *  => i   j k
                 */
                while(j < k) {
                    if(nums[i] + nums[j] + nums[k] == 0) {
                        List<Integer> sub_result = new ArrayList<Integer>();
                        sub_result.add(nums[i]);
                        sub_result.add(nums[j]);
                        sub_result.add(nums[k]);
                        result.add(sub_result);
                        
                        //做完之後一定要移動指針(表示做過了)
                        j ++;
                        k --;
                        
                        //避免指針移到重複元素,並確保j < k避免指針錯誤
                        while(j < k && nums[j] == nums[j - 1]) {
                            j ++;
                        }
                        //避免指針移到重複元素,並確保j < k避免指針錯誤
                        while(j < k && nums[k] == nums[k + 1]) {
                            k --;
                        }
                    //移動右指針k
                    } else if(nums[i] + nums[j] + nums[k] > 0) {
                        k --;
                    //移動右指針j
                    } else if(nums[i] + nums[j] + nums[k] < 0) {
                        j ++;
                    }
                }
            }
        }
        
        return result;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值