LeetCode(15)-3sum 三数之和

Given an array S of n integers, are there elements a, b, c 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)
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

/*
Given an array S of n integers, are there elements a, b, c 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)
*/
注意去重

public class sum3 {
    HashMap<Integer,Integer> map=new HashMap<Integer, Integer>();
    void add(int value){
       if(map.containsKey(value)){
           map.put(value,map.get(value)+1);
       }
       else{
           map.put(value,1);
       }
    }
    public ArrayList<ArrayList<Integer>> threeSum(int[] num) {
        ArrayList<ArrayList<Integer>> res=new ArrayList<ArrayList<Integer>>();

        if(num==null)
            return  res;
        //先排序
        Arrays.sort(num);
        for(int i=0;i<num.length-2;i++){
            if(num[i]>0)
                break;
            //再选两个数
            int left=i+1;
            int right=num.length-1;
            int target=0-num[i];
            //避免重复
            if(i>0&&num[i]==num[i-1]){
                continue;
            }
            while(left<right){
                if(num[left]+num[right]==target){
                    ArrayList<Integer> temp=new ArrayList<>();
                    temp.add(num[i]);
                    temp.add(num[left]);
                    temp.add(num[right]);  //一个solution
                    res.add(temp);
                    left++;
                    right--;
                    //防止重复
                    while (left<right&&num[left]==num[left-1]){
                        ++left;
                    }
                    while (left<right&&num[right]==num[right+1]){
                        --right;
                    }

                }
                else if(num[left]+num[right]<target){
                    left++;
                }
                else {
                    right--;
                }
            }
            }
            return res;

    }
    public static void main(String[] args) {
        sum3 sum=new sum3();
        int[] s={-1,0,1,2,-1,-4};
        ArrayList<ArrayList<Integer>> ans=sum.threeSum(s);
        System.out.println(ans);
    }
}

hashmap解法类似

链接:https://www.nowcoder.com/questionTerminal/345e2ed5f81d4017bbb8cc6055b0b711
来源:牛客网

class Solution {
    public List<List<Integer>> threeSum(int[] num) {
        List<List<Integer>> arrayLists = new ArrayList<List<Integer>>();
        Arrays.sort(num);
        HashMap<Integer,Integer> map = new HashMap<Integer,Integer>(num.length); //用hashmap方便查找

        for(int i=0;i<num.length;i++){  //放入到hashmap中
            map.put(num[i],i);
        }

        for(int i=0;i<num.length;i++){
            if(i>0&&num[i]==num[i-1]){  //每次第一个数挑选一个不同的数
                continue;
            }
            if(num[i]>0){  //剪枝
                return arrayLists;
            }
            for(int j=i+1;j<num.length;j++){
                if(num[j]==num[j-1]&&j-i>1){  //排除第二数和第一个数重复很多遍 例如:-1 -1 -1 ,只需要第一个和第二相加就可以,第三个可以忽略
                    continue;
                }
                if(num[i]+num[j]>0){
                    break;
                }
                int temp = -(num[i] + num[j]);
                if(map.containsKey(temp)&&map.get(temp)>j){  //保证三个数依次递增,这样可以不重复
                    ArrayList<Integer> arrayList =new ArrayList<Integer>();
                    arrayList.add(num[i]);
                    arrayList.add(num[j]);
                    arrayList.add(temp);
                    arrayLists.add(arrayList);
                }
            }
        }


        return arrayLists;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值