solution.md 4.9 KB
Newer Older
1
# 最接近的三数之和
F
fix bug  
feilong 已提交
2

3
<p>给定一个包括&nbsp;<em>n</em> 个整数的数组&nbsp;<code>nums</code><em>&nbsp;</em>和 一个目标值&nbsp;<code>target</code>。找出&nbsp;<code>nums</code><em>&nbsp;</em>中的三个整数,使得它们的和与&nbsp;<code>target</code>&nbsp;最接近。返回这三个数的和。假定每组输入只存在唯一答案。</p><p>&nbsp;</p><p><strong>示例:</strong></p><pre><strong>输入:</strong>nums = [-1,2,1,-4], target = 1<strong><br />输出:</strong>2<strong><br />解释:</strong>与 target 最接近的和是 2 (-1 + 2 + 1 = 2) 。</pre><p>&nbsp;</p><p><strong>提示:</strong></p><ul>	<li><code>3 &lt;= nums.length &lt;= 10^3</code></li>	<li><code>-10^3&nbsp;&lt;= nums[i]&nbsp;&lt;= 10^3</code></li>	<li><code>-10^4&nbsp;&lt;= target&nbsp;&lt;= 10^4</code></li></ul>
每日一练社区's avatar
每日一练社区 已提交
4
<p>以下<span style="color:red">错误</span>的选项是?</p>
F
fix bug  
feilong 已提交
5

6
## aop
F
fix bug  
feilong 已提交
7

F
fix bug  
feilong 已提交
8

9
### before
F
fix bug  
feilong 已提交
10

F
fix bug  
feilong 已提交
11

12 13 14 15 16
```cpp
#include <bits/stdc++.h>

using namespace std;
```
F
fix bug  
feilong 已提交
17

18
### after
F
fix bug  
feilong 已提交
19

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
```cpp
int main()
{
    Solution sol;
    int arr[] = {-1, 2, 1, -4};
    int target = 1;
    int length1 = sizeof(arr) / sizeof(arr[0]);
    vector<int> nums(arr, arr + length1);
    int res;
    res = sol.threeSumClosest(nums, target);
    cout << res;
    return 0;
}
```

## 答案
F
fix bug  
feilong 已提交
36

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
```cpp
class Solution
{
public:
    int threeSumClosest(vector<int> &nums, int target)
    {
        if (nums.size() < 3)
            ;
        runtime_error("No Solution");
        sort(nums.begin(), nums.end());
        int res = nums[0] + nums[1] + nums[2];
        int dValue = abs(target - res);
        int l, r, sum;
        for (int i = 0; i < nums.size() - 2; ++i)
        {
            l = i + 1;
            r = nums.size() - 1;
            if (3 * nums[i] > target)
                break;
            while (l < r)
            {
                sum = nums[i] + nums[l] + nums[r];
                if (sum < target)
                    --l;
                else if (sum > target)
                    ++r;
                else
                    return target;
                if (abs(target - sum) < dValue)
                {
                    res = sum;
                    dValue = abs(target - sum);
                }
            }
        }
        return res;
    }
};
```
## 选项

F
fix bug  
feilong 已提交
78

79
### A
F
fix bug  
feilong 已提交
80

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
```cpp
class Solution
{
public:
    int threeSumClosest(vector<int> &nums, int target)
    {
        int l = nums.size();
        int best = nums[0] + nums[1] + nums[2];
        for (int i = 0; i < l - 2; i++)
        {
            for (int j = i + 1; j < l - 1; j++)
            {
                for (int k = j + 1; k < l; k++)
                {
                    int t = nums[i] + nums[j] + nums[k];
                    if (abs(t - target) < abs(best - target))
                    {
                        best = t;
                    }
                }
            }
        }
        return best;
    }
};
```

### B
F
fix bug  
feilong 已提交
109

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
```cpp
class Solution
{
public:
    int threeSumClosest(vector<int> &nums, int target)
    {
        sort(nums.begin(), nums.end());
        if (nums.size() < 3)
            return {};
        int closest = target + 10000000;
        for (int i = 0; i < nums.size() - 2; i++)
        {
            int fix = nums[i];
            if (fix > target && abs(fix + nums[i + 1] + nums[i + 2] - target) > abs(closest - target))
                break;

            for (int j = i + 1; j < nums.size() - 1; j++)
            {
                for (int k = j + 1; k < nums.size(); k++)
                {
                    int temp = abs(target - fix - nums[j] - nums[k]);
                    if (temp < abs(closest - target))
                        closest = fix + nums[j] + nums[k];
                }
            }
        }
        return closest;
    }
};
```

### C
F
fix bug  
feilong 已提交
142

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
```cpp
class Solution
{
public:
    int threeSumClosest(vector<int> &nums, int target)
    {
        int len = nums.size();
        int min = target + 1000000;
        sort(nums.begin(), nums.end());
        for (int i = 0; i < len; i++)
        {
            int fir = nums[i];
            int r = len - 1;
            int l = i + 1;
            if (i > 0 && fir == nums[i - 1])
                continue;
            if (l >= r)
                break;
            while (l < r)
            {
                if (nums[l] + nums[r] + fir == target)
                    return target;
                if (nums[l] + nums[r] < target - fir)
                {
                    int temp = target - fir - nums[r] - nums[l];
                    if (temp < abs(min - target))
                        min = nums[l] + nums[r] + fir;
                    l++;
                }
                else if (nums[l] + nums[r] > target - fir)
                {
                    int temp = nums[l] + nums[r] + fir - target;
                    if (temp < abs(min - target))
                        min = nums[l] + nums[r] + fir;
                    r--;
                }
            }
        }
        return min;
    }
};
```