题目:3Sum Closest

Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

For example, given array S = {-1 2 1 -4}, and target = 1.

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

分析:直接上三重循环,时间复杂度O(n^3),不过这样会超时的。所以我们应该先排序,然后再写三重循环,由于数组是递增的,所以当我们发现当前数字已经大于abs(target-closest)时,就可以直接break掉后面的了。

代码:

public class Solution {
    public int threeSumClosest(int[] num, int target) {
        int i, j, k;
        int a, b, c, t;
        int r = 0;
        int m = Integer.MAX_VALUE;
        Arrays.sort(num);

        for (i = 0; i < num.length; i++) {
            a = num[i];
            if (a - target > m) break;
            for (j = i + 1; j < num.length; j++) {
                b = num[j];
                if (a + b - target > m) break;
                for(k = j + 1; k < num.length; k++) {
                    c = num[k];
                    t = a + b + c - target;
                    if (t > m) break;
                    if (Math.abs(t) <  m) {
                        m = Math.abs(t);
                        r = a + b + c;
                    }
                }
            }
        }
        return r;
    }
}

本以为这个代码应该是通过是没问题的,结果还是被打回来了。

缺心眼的毛病改不了啊。

由于数组是递增的,于是当a - target > m时,我便认为a加上位于它数组后面的任何数字都会比当前的a值大,因此直接break掉了。

但是,[-10, -5, -3, -2, 0, 5, 12],当a为-5时,加上它下一个数-3得到-8就比a自己小啊。

于是所有的剪枝判断都加上了大于0的判断。

public class Solution {
    public int threeSumClosest(int[] num, int target) {
        int i, j, k;
        int a, b, c, t;
        int r = 0;
        int m = Integer.MAX_VALUE;
        Arrays.sort(num);

        for (i = 0; i < num.length; i++) {
            a = num[i];
            if (a > 0 && a - target > m) break;
            for (j = i + 1; j < num.length; j++) {
                b = num[j];
                if (a + b > 0 && a + b - target > m) break;
                for(k = j + 1; k < num.length; k++) {
                    c = num[k];
                    t = a + b + c - target;
                    if (t > 0 && t > m) break;
                    if (Math.abs(t) < m) {
                        m = Math.abs(t);
                        r = a + b + c;
                    }
                }
            }
        }
        return r;
    }
}

ssnau
1.5k 声望98 粉丝

负能量职业打码师


引用和评论

0 条评论