力扣【15】三数之和

题目:

给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有和为 0 且不重复的三元组。

注意:答案中不可以包含重复的三元组。

 

示例 1:

输入:nums = [-1,0,1,2,-1,-4]
输出:[[-1,-1,2],[-1,0,1]]
示例 2:

输入:nums = []
输出:[]
示例 3:

输入:nums = [0]
输出:[]

题解:

写在前面:set.add()实际执行的是 map 的方法,因为map中的key是不允许重复的,所以set中的元素不能重复。

另外:HashSet和HashMap都不保证顺序,LinkedHashSet和LinkedHashMap能保证顺序。

方法一:

小知识:数组下标可以为负,但是十分不建议这样写。

分析
给出的示例中定义了一维数组a
int a[5];
对于数组来说,a[-1]的写法编译器是通过的,其代表a[0]的前一个元素。但是像a[-1]这样的写法是十分危险的,因为a[-1]的地址是不确定的,万一它指向了操作系统的某块内存,则可能影响到操作系统的运行。所以不建议数组下标为负的写法。

package test;

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

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> res = new ArrayList();
        for (int i = 0; i < nums.length; i++) {
            int target = 0 - nums[i];
            int l = i + 1;
            int r = nums.length - 1;
            if (nums[i] > 0)
                break;
            if (i == 0 || nums[i] != nums[i - 1]) {//这个地方一定要加,第i个不能重复
                while (l < r) {
                    if (nums[l] + nums[r] == target) {
                        res.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] < target)
                        l++;
                    else
                        r--;
                }
            }
        }
        return res;
    }
}
public class Main{
    public static void main (String []args){
        int[] a = {-1, 0, 1, 2, -1, -4};
        Solution p = new Solution();
        List<List<Integer>> b = p.threeSum(a);
        System.out.println("结果:"+b);
    }
}

方法二:暴力,首先使用Array.sort()排序,三个for循环,然后加入不重复的set集合,超时了。

package test;

import java.util.*;

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        if (nums == null || nums.length <= 2) {
            return Collections.emptyList();
        }
        Arrays.sort(nums);
        Set<List<Integer>> result = new HashSet<>();
        for (int i = 0; i < nums.length; i++) {
            for (int j = i+1; j < nums.length; j++) {
                for (int k = j+1; k < nums.length; k++) {
                    if (nums[i] + nums[j] + nums[k] == 0) {
                        List<Integer> value = Arrays.asList(nums[i], nums[j], nums[k]);//数组转换成List集合可以使用Array.asList(a,b,c),也可以for循环一个个加
                        result.add(value);
                    }
                }
            }
        }
        return new ArrayList<>(result);
    }
}


public class Main{
    public static void main (String []args){
        int[] a = {-1, 0, 1, 2, -1, -4};
        Solution p = new Solution();
        List<List<Integer>> b = p.threeSum(a);
        System.out.println("结果:"+b);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值