zoukankan      html  css  js  c++  java
  • Leetcode: 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).

    这道题跟3Sum很像,区别就是要维护一个最小的diff,求出和目标最近的三个和。brute force时间复杂度为O(n^3),优化的解法是使用排序之后夹逼的方法,总的时间复杂度为O(n^2+nlogn)=(n^2),空间复杂度是O(n)。

    第二遍做法:

     1 public class Solution {
     2     public int threeSumClosest(int[] num, int target) {
     3         int result = num[0] + num[1] + num[num.length - 1];
     4         Arrays.sort(num);
     5         for (int i = 0; i < num.length - 2; i++) {
     6             int start = i + 1, end = num.length - 1;
     7             while (start < end) {
     8                 int sum = num[i] + num[start] + num[end];
     9                 if (sum > target) {
    10                     end--;
    11                 } else {
    12                     start++;
    13                 }
    14                 if (Math.abs(sum - target) < Math.abs(result - target)) {
    15                     result = sum;
    16                 }
    17             }
    18         }
    19         return result;
    20     }
    21 }

    第一遍做法:

     1 public class Solution {
     2     public int threeSumClosest(int[] num, int target) {
     3         if (num == null || num.length < 3) return 0;
     4         Arrays.sort(num);
     5         int minDiff = num[0] + num[1] + num[2] - target;
     6         int diff = 0;
     7         for (int i=num.length-1; i>=2; i--) {
     8             if (i<num.length-1 && num[i]==num[i+1]) continue;
     9             diff = twoSumClosest(num, 0, i-1, target-num[i]);
    10             if (Math.abs(diff) < Math.abs(minDiff)) {
    11                 minDiff = diff;
    12             }
    13         }
    14         return minDiff + target;
    15     }
    16     
    17     public int twoSumClosest(int[] num, int l, int r, int tar) {
    18         int minDif = num[l] + num[r] - tar;
    19         int dif = 0;
    20         while (l < r) {
    21             dif = num[l] + num[r] - tar;
    22             if (dif == 0) return dif;
    23             if (Math.abs(dif) < Math.abs(minDif)) {
    24                 minDif = dif;
    25             }
    26             if (dif < 0) {
    27                 l++;
    28             }
    29             else {
    30                 r--;
    31             }
    32         }
    33         return minDif;
    34     }
    35 }
  • 相关阅读:
    由于取消了脚本支持,无法查看访问统计,也办法放gg广告了,本站更新速度降低.
    关于ASP循环表格的问题之解答
    STL中map用法详解
    vector与list的区别
    mysql联合索引 sql索引使用
    如何让linux/Centos 32位支持大于4G内存
    mysql事务
    Mysql InnoDB中的查询事务模式与锁定select ..for update
    linux 查看系统版本
    如何在64位的Linux中运行32位的应用程序
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/4012459.html
Copyright © 2011-2022 走看看