15. 3Sum


Given an array nums of n integers, are there elements abc in nums 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.

Example:

Given array nums = [-1, 0, 1, 2, -1, -4],

A solution set is:
[
  [-1, 0, 1],
  [-1, -1, 2]
]
给定一个包含  n  个整数的数组  nums ,判断  nums  中是否存在三个元素  a,b,c , 使得  a + b + c =  0 ?找出所有满足条件且不重复的三元组。

    挺有趣的一题,反复确认了一下,题目没有时间复杂度的要求,那么三层循环依次寻找就成为了最简单的写法了,有兴趣的同学可以自己写写看,这里就不写了。虽然题目没有要求,但是身为程序猿好歹还是要有点极客精神,单纯的使用枚举法就有点low了。

    观察一下数组,发现例子给的是无序数组,但是假如我们把数组变为有序确可以帮助我们优化算法,那么就假定第一步是对数组进行排序。

//不想写排序算法的同学可以这样。
    //Arrays.sort(nums);
    public void sort(int[] nums, int left, int right){

        if (left > right)
            return;

        int tempLeft = left;
        int tempRight = right;

        int x = nums[left];

        while (left < right){

            while (left<right && x < nums[right])
                right--;
            if (left < right)
                nums[left++] = nums[right];

            while (left<right && x > nums[left])
                left++;

            if (left < right)
                nums[right--] = nums[left];
        }

        nums[left] = x;

        sort(nums, tempLeft, tempRight - 1);
        sort(nums, tempLeft + 1, tempRight);
    }

排序之后,以上面那个例子为例

nums = [-4,-1,-1,0, 1, 2]

如nums[0]+nums[1]+nums[5]<0那么我们明确知道nums[5]前面的都比他小,则本次循环可以跳出了

如nums[1]+nums[2]+nums[4]==0,那么还需要继续往前寻找,直到出现上述情况。

public List<List<Integer>> threeSum(int[] nums) {
        
        sort(nums, 0, nums.length - 1);

        List<List<Integer>> resultList = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            int left = i + 1;
            int right = nums.length - 1;
            while (left < right){
                if (nums[i] + nums[left] + nums[right] == 0){
                    resultList.add(Arrays.asList(nums[i], nums[left], nums[right]));
                    right--;
                }else if (nums[i] + nums[left] + nums[right] < 0){
                    left++;
                }else {
                    right--;
                }
            }
        }
        return resultList;
    }

写完了上面的代码运行,发现和例子中的结果不一样,例子中还需要去重。加上去掉重复数据的代码。其实也可以得到resultList之后再进行去重,但是如此就多进行了循环,所以需要在循环的过程中就去掉重复数据。

    public List<List<Integer>> threeSum(int[] nums) {

        sort(nums, 0, nums.length - 1);

        List<List<Integer>> resultList = new ArrayList<>();
        for (int i = 0; i < nums.length - 2; i++) {
            int left = i + 1;
            int right = nums.length - 1;
            while (left < right){
                if (nums[i] + nums[left] + nums[right] == 0){
                    resultList.add(Arrays.asList(nums[i], nums[left], nums[right]));
                    right--;
                    left++;

                    while (left < right && nums[left] == nums[left - 1])
                        left++;
                    while (left < right && nums[right] == nums[right + 1])
                        right--;
                }else if (nums[i] + nums[left] + nums[right] < 0){
                    left++;
                }else {
                    right--;
                }
            }

            while (i < nums.length - 2 && nums[i] == nums[i+1])
                i++;
        }
        return resultList;
    }


在LeetCode提交过程中出现了一件很尴尬的事情,如果对JDK源码熟悉的同学应该已经发现了,我特么写的快排在复杂数据面前并没有Arrays.sort()来的效率高,导致提交的时候出现了超时,很尴尬啊。

简单看了下Arrays.sort()源码

static void sort(int[] a, int left, int right,
                     int[] work, int workBase, int workLen) {
        // Use Quicksort on small arrays
        if (right - left < QUICKSORT_THRESHOLD) {
            sort(a, left, right, true);
            return;
        }

....

private static void sort(int[] a, int left, int right, boolean leftmost) {
        int length = right - left + 1;

        // Use insertion sort on tiny arrays
        if (length < INSERTION_SORT_THRESHOLD) {
            if (leftmost) {

JDK中根据数组的长度采用了快排、插入、等多种排序方法,血的教训告诉我们,实际开发过程中尽量避免重复的造轮子,当然学习的过程中就不要怕犯错了。


点击查看更多解法


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值