码迷,mamicode.com
首页 > 其他好文 > 详细

16. 3Sum Closest

时间:2020-02-18 09:54:23      阅读:55      评论:0      收藏:0      [点我收藏+]

标签:排序   ati   main   each   star   时间   alt   题目   mat   

1.题目描述

英文版:

Given an array nums of n integers and an integer target, find three integers in

nums such that the sum is closest to target. Return the sum of the three integers.

You may assume that each input would have exactly one solution.

Example:

Given array nums = [-1, 2, 1, -4], and target = 1.

The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

中文版:

给一个长度为n的整型数组nums和一个目标值。在数组中寻找三个元素,使他们的和最接近目标值,返回这三个元素的和。

例如:

数组 nums = [-1, 2, 1, -4]

目标值 target = 1

最接近目标值得三个元素之和为 2 (-1 + 2 + 1)

2.解法

2.1 解法1

思路:

使用最简单明了的暴力破解法。不过时间复杂度非常高,可以见下图。

首先,定义两个变量,closest 用来存储最接近tartget的三元组之和的绝对值,result 用来存储最接近tartget的三元组之和。

三重循环遍历每一组三元组,求出他们的绝对值并与closest对比,如果小于就将它赋值给closest并且把三元组的和也存到result中,

最后返回result。

    public static int threeSumClosest1(int[] nums, int target) {
        int closest = Integer.MAX_VALUE;
        int result = 0;
        for(int i = 0;i < nums.length - 2;i++){
            for(int j = i + 1;j < nums.length - 1;j++){
                for(int k = j + 1;k < nums.length;k++){
                    int sum = nums[i] + nums[j] + nums[k];
                    if(closest > Math.abs(target - (sum))){
                        closest = Math.abs(target - (sum));
                        result = sum;
                    }
                }
            }
        }
        return result;
    }

在LeetCode上运行的结果:

技术图片

2.2 解法2

思路:

这种做法是在3Sum题目解法的基础上进行修改。首先在遍历之前也选对数组进行排序,方便后面操作。排好序之后,从左边到右边,先确定i位置对应的数。然后再寻找另外两个数,其中一个数的位置start从i+1开始往后移,另一个数的位置end从nums.length - 1开始往前移。

注意的点:

1、只循环到倒数第三个数即可
2、sum == target时直接返回,因为题目说只用唯一的三元组和最接近target
3、sum < target 时start往后移
4、sum > target 时end往前移

代码实现:
     public static int threeSumClosest2(int[] nums, int target) {
        Arrays.sort(nums);
        int closest = Integer.MAX_VALUE;
        int result = 0;
        for(int i = 0;i < nums.length - 2;i++){ //循环到倒数第三个
            int start = i + 1;
            int end = nums.length - 1;
            while (start < end) {
                int sum = nums[i] + nums[start] + nums[end];
                if(closest > Math.abs(target - (sum))){
                    closest = Math.abs(target - (sum)); //三数和跟给定值之间的差的绝对值最小
                    result = sum;
                }

                if(sum == target){
                    return result;
                }else if(sum < target){
                    start++;
                }else {
                    end--;
                }
            }
        }
        return result;
    }

在LeetCode上运行的结果:

技术图片

3.测试

在本地,我只是简单写了一个main函数来对它进行测试。但是这些代码都是在LeetCode上运行通过了的。

    public static void main(String[] args) {
        int[] nums = {-1, 2, 1, -4};
        int target = 1;
        int result = threeSumClosest2(nums,target);
        System.out.println(result);
    }
欢迎关注个人公众号,可直接扫描以下二维码或微信搜索“阿毛聊技术”。

技术图片

16. 3Sum Closest

标签:排序   ati   main   each   star   时间   alt   题目   mat   

原文地址:https://www.cnblogs.com/limaodeng/p/12324559.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!