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 };
  • 相关阅读:
    【Android】开发优化之——调优工具:TrackView,Method Profiling
    MapReduce:具体解释Shuffle过程
    Kafka编程实例
    常用的语句
    CDN
    iOS中的固定 高度
    iPhone手机屏幕的尺寸180330更新
    UITextField
    iOS基本网络请求
    Xcode界面简介
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/3567707.html
Copyright © 2011-2022 走看看