public class Solution {
    public int threeSumClosest(int[] num, int target) {
        int result = num[0] + num[1] + num[num.length - 1];
        Arrays.sort(num);
        for (int i = 0; i < num.length - 2; i++) {
            int start = i + 1, end = num.length - 1;
            while (start < end) {
                int sum = num[i] + num[start] + num[end];
                if (sum > target) {
                    end--;
                } else {
                    start++;
                }
                if (Math.abs(sum - target) < Math.abs(result - target)) {
                    result = sum;
                }
            }
        }
        return result;
    }
}

相关文章:

  • 2021-08-08
  • 2021-05-31
  • 2021-05-24
  • 2022-02-25
  • 2022-12-23
  • 2022-12-23
  • 2021-09-24
猜你喜欢
  • 2021-05-22
  • 2022-01-30
  • 2021-11-29
  • 2022-01-07
  • 2021-10-16
  • 2022-02-25
相关资源
相似解决方案