solution.md 5.0 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 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 78 79 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 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 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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
# 三数之和
以下错误的选项是?
## aop
### before
```cpp
#include <vector>
#include <iostream>
#include <set>

using namespace std;
```
### after
```cpp
int main()
{
    Solution sol;
    int arr[] = {-1,0,1,2,-1,-4};
    int length1 = sizeof(arr) / sizeof(arr[0]);
    vector<int> nums(arr, arr + length1);
    vector<vector<int> > res;
    res = sol.threeSum(nums);

    for (auto i : res)
    {
        for (auto j : i)
            cout << j << ' ';
        cout << endl;
    }
    return 0;
}

```

## 答案
```cpp
class Solution
{
public:
    vector<vector<int>> threeSum(vector<int> &nums)
    {
        sort(nums.begin(), nums.end());
        int len = nums.size();
        vector<vector<int>> ans;
        if (nums.empty())
            return {};
        for (int i = 0; i < len; ++i)
        {
            if (i > 0 && nums[i] == nums[i - 1])
                continue;
            int newtar = -nums[i];
            int low = i + 1;
            int high = len - 1;
            while (low < high)
            {
                if (nums[low] + nums[high] == newtar)
                {
                    ans.push_back({nums[i], nums[low], nums[high]});
                    while (low < high && nums[low] == nums[low + 1])
                        ++low;
                    while (low < high && nums[high] == nums[high - 1])
                        --high;
                }
                else
                    high--;
            }
        }
        return ans;
    }
};
```
## 选项

### A
```cpp
class Solution
{
public:
    vector<vector<int>> threeSum(vector<int> &nums)
    {
        sort(nums.begin(), nums.end());
        if (nums.size() < 3 || nums.front() > 0 || nums.back() < 0)
            return {};
        vector<vector<int>> res;
        for (int i = 0; i < nums.size(); i++)
        {
            int fix = nums[i];
            if (fix > 0)
                break;
            if (i > 0 && fix == nums[i - 1])
                continue;
            int l = i + 1;
            int r = nums.size() - 1;
            while (l < r)
            {
                if (nums[l] + nums[r] == -fix)
                {
                    if (l == i + 1 || r == nums.size() - 1)
                    {
                        res.push_back(vector<int>{nums[i], nums[l], nums[r]});
                        l++;
                        r--;
                    }
                    else if (nums[l] == nums[l - 1])
                        l++;
                    else if (nums[r] == nums[r + 1])
                        r--;
                    else
                    {
                        res.push_back(vector<int>{nums[i], nums[l], nums[r]});
                        l++;
                        r--;
                    }
                }
                else if (nums[l] + nums[r] < -fix)
                    l++;
                else
                    r--;
            }
        }
        return res;
    }
};
```

### B
```cpp
class Solution
{
public:
    static bool cmp(const int &a, const int &b)
    {
        return a < b;
    }
    vector<vector<int>> threeSum(vector<int> &nums)
    {
        sort(nums.begin(), nums.end(), cmp);
        vector<int>::iterator it;
        vector<vector<int>> res;
        set<vector<int>> a;
        for (int i = 0; i < nums.size(); i++)
        {
            if (nums[i] > 0)
                break;
            for (int j = i + 1; j < nums.size(); j++)
            {
                it = find(nums.begin() + j + 1, nums.end(), 0 - nums[i] - nums[j]);
                if (it != nums.end())
                {
                    vector<int> temp;
                    temp.push_back(nums[i]);
                    temp.push_back(nums[j]);
                    temp.push_back(*it);
                    a.insert(temp);
                }
            }
        }
        for (auto k : a)
        {
            res.push_back(k);
        }
        return res;
    }
};
```

### C
```cpp
class Solution
{
public:
    vector<vector<int> > threeSum(vector<int> &nums)
    {
        if (nums.size() < 3)
            return {};
        vector<vector<int> > res;
        set<vector<int> > ret;
        for (int i = 0; i < nums.size() - 2; i++)
        {
            for (int j = i + 1; j < nums.size() - 1; j++)
            {
                for (int k = j + 1; k < nums.size(); k++)
                {
                    if (nums[i] + nums[k] + nums[j] == 0)
                    {
                        vector<int> temp;
                        int a = (nums[i] < nums[j] ? nums[i] : nums[j]) < nums[k] ? (nums[i] < nums[j] ? nums[i] : nums[j]) : nums[k];
                        int b = (nums[i] > nums[j] ? nums[i] : nums[j]) > nums[k] ? (nums[i] > nums[j] ? nums[i] : nums[j]) : nums[k];
                        int c = 0 - a - b;
                        temp.push_back(a);
                        temp.push_back(c);
                        temp.push_back(b);
                        ret.insert(temp);
                    }
                }
            }
        }
        for (auto it : ret)
        {
            res.push_back(it);
        }
        return res;
    }
};
```