zoukankan      html  css  js  c++  java
  • LeetCode

    3Sum Closest

    2014.2.25 19:02

    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).

    Solution:

      This problem is a variation from 3Sum. In that problem, an O(n^2) algorithm was mentioned.

      At first I tried to sort the array and do binary searches, but got an TLE. Then I knew it would still be O(n^2) in time complexity.

      There has to be a minor adjustment for the code of "3Sum". When performing the linear scan from both ends, we not only look for exact match, but also record every sum that is closer to the target. At the end of the search, there will either be a closest sum to the target, or the target value itself.

      Time complexity is O(n^2), space complextiy is O(1).

      The array is not sorted, so extra O(n * log(n)) time is needed for sorting.

    Accepted code:

     1 // 2TLE, 1AC, O(n^2) solution, linear scan is faster than binary search.
     2 #include <algorithm>
     3 using namespace std;
     4 
     5 class Solution {
     6 public:
     7     int threeSumClosest(vector<int> &num, int target) {
     8         int n = (int)num.size();
     9         if (n < 3) {
    10             return 0;
    11         }
    12         
    13         int i, j, k;
    14         int res;
    15         int sum;
    16         
    17         // sort the array for linear scan
    18         sort(num.begin(), num.end());
    19         // intialize with whatever result here
    20         res = num[0] + num[1] + num[2];
    21         for (i = 0; i < n; ++i) {
    22             j = i + 1;
    23             k = n - 1;
    24             while (j < k) {
    25                 sum = num[i] + num[j] + num[k];
    26                 if (sum < target) {
    27                     ++j;
    28                     if (myabs(sum - target) < myabs(res - target)) {
    29                         res = sum;
    30                     }
    31                 } else if (sum > target) {
    32                     --k;
    33                     if (myabs(sum - target) < myabs(res - target)) {
    34                         res = sum;
    35                     }
    36                 } else {
    37                     return target;
    38                 }
    39             }
    40         }
    41         
    42         return res;
    43     }
    44 private:
    45     int myabs(const int n) {
    46         return (n >= 0 ? n : -n);
    47     }
    48 };
  • 相关阅读:
    wince开发_摩托罗拉MC3100_打开条码设置
    【Scala类型系统】自身类型(self type)引用
    Scala 基础新手教程
    ActiveMQ消息队列的使用及应用
    nginx配置服务器负载均衡
    ActiveMQ_Linux安装
    js调试工具Console命令详解
    微信JS-SDK选择相册或拍照并上传PHP实现
    微信公众平台开发接口PHP SDK完整版
    微信JS接口汇总及使用详解
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/3567707.html
Copyright © 2011-2022 走看看