Java LeetCode每日一题-从易到难带你领略算法的魅力(十六):三数之和

1.题目要求

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

2.题目示例

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

3.提示

  • 0 <= nums.length <= 3000
  • 105 <= nums[i] <= 105
  • So, we essentially need to find three numbers x, y, and z such that they add up to the given value. If we fix one of the numbers say x, we are left with the two-sum problem at hand!
    我们实际上需要找到三个数x y和z使它们加起来等于给定的值。如果我们固定其中一个数字,比如x,我们就剩下了两个和的问题!
  • For the two-sum problem, if we fix one of the numbers, say x, we have to scan the entire array to find the next number y which is value - x
    where value is the input parameter. Can we change our array somehow so that this search becomes faster?
    对于二和问题,如果我们固定一个数字,比如x,我们必须扫描整个数组来找到下一个数字y,它的值是- x
    其中value为输入参数。我们能不能改变数组让搜索变得更快?
  • The second train of thought for two-sum is, without changing the array, can we use additional space somehow? Like maybe a hash map to speed up the search?
    二和运算的第二种思路是,在不改变数组的情况下,我们可以使用额外的空间吗?比如用哈希映射来加速搜索?

4.解题

4.1 解题思路

  1. 根据题目要求,首先要考虑到去重的问题,那么我们首先可以对数组进行一个从小到大的排序。顺序数组方便去重。
  2. 我们可以假设固定了第一个a后(在数组已经排序的情况下),再去找到b=a+1和数组的c=len-1,然后把他们相加,如果不等于0就a不变然后b变为a+2,直到bc重合则退出循环。如果相加等于0则,加入返回数组对象
  3. 这里要注意的一点是,当a>0的时候,那么a+b+c肯定大于0
  4. 如果当前下标数据是i=i+1时,则跳过本次循环
nums = [-1,0,1,2,-1,-4]
--------------------------------
[-4,-1,-1,0,1,2]	a+b+c!=0
  ↑  ↑        ↑
  a  b        c
[-4,-1,-1,0,1,2]	a+b+c!=0
  ↑     ↑     ↑
  a     b     c
[-4,-1,-1,0,1,2]	a+b+c!=0
  ↑       ↑   ↑
  a       b   c
[-4,-1,-1,0,1,2]	a+b+c!=0
  ↑         ↑ ↑
  a         b c
[-4,-1,-1,0,1,2]	a+b+c=0
     ↑  ↑     ↑
     a  b     c
。。。。。。。。。。

4.2 业务代码

class Solution {
    public static List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> ans = new ArrayList();
        int len = nums.length;
        if(nums == null || len < 3) return ans;
        Arrays.sort(nums); // 排序
        for (int i = 0; i < len ; i++) {
            if(nums[i] > 0) break; // 如果当前数字大于0,则三数之和一定大于0,所以结束循环
            if(i > 0 && nums[i] == nums[i-1]) continue; // 去重
            int L = i+1;
            int R = len-1;
            while(L < R){
                int sum = nums[i] + nums[L] + nums[R];
                if(sum == 0){
                    ans.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 (sum < 0) L++;
                else if (sum > 0) R--;
            }
        }        
        return ans;
    }
}

4.3 运行结果

结果

5.总结

  • 如果大家有新的想法欢迎在下面留言
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

地球村公民

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

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

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

打赏作者

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

抵扣说明:

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

余额充值