solution.md 3.2 KB
Newer Older
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
# 合并区间
以下错误的选项是?
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp

```

## 答案
```cpp
class Solution
{
public:
    vector<vector<int>> merge(vector<vector<int>> &intervals)
    {
        vector<vector<int>> res;
        sort(intervals.begin(), intervals.end(), [](const auto &u, const auto &v)
             {
                 if (u[0] == v[0])
                     return u[1] > v[1];
                 else
                     return u[0] < v[0];
             });
        auto it = intervals.begin();
        int first = (*it)[0], second = (*it)[1];
        for (it++; it != intervals.end(); it++)
        {
            if ((*it)[0] <= second)
                second = max(second, (*it)[0]);
            else
            {
                res.push_back({first, second});
                first = (*it)[0];
                second = (*it)[1];
            }
        }
        res.push_back({first, second});
        return res;
    }
};
```
## 选项

### A
```cpp
class Solution
{
public:
    vector<vector<int>> merge(vector<vector<int>> &intervals)
    {
        vector<vector<int>> res;
        sort(intervals.begin(), intervals.end());
        for (auto it = intervals.begin(); it != intervals.end(); it++)
        {
            int first = (*it)[0], second = (*it)[1];
            if (!res.size() || res.back()[1] < first)
                res.push_back({first, second});
            else
                res.back()[1] = max(res.back()[1], second);
        }
        return res;
    }
};
```

### B
```cpp
class Solution
{
public:
    vector<vector<int>> merge(vector<vector<int>> &intervals)
    {
        sort(intervals.begin(), intervals.end());
        vector<vector<int>> res;
        for (int i = 0; i < intervals.size();)
        {
            int t = intervals[i][1];
            int j = i + 1;
            while (j < intervals.size() && intervals[j][0] <= t)
            {
                t = max(t, intervals[j][1]);
                j++;
            }
            res.push_back({intervals[i][0], t});
            i = j;
        }
        return res;
    }
};
```

### C
```cpp
class Solution
{
public:
    static bool cmp(const vector<int> &a, const vector<int> &b)
    {
        if (a[0] == b[0])
            return a[1] > b[1];
        return a[0] < b[0];
    }
    vector<vector<int>> merge(vector<vector<int>> &intervals)
    {
        if (intervals.empty())
            return intervals;
        vector<vector<int>> res;
        int count = 0;
        sort(intervals.begin(), intervals.end(), cmp);
        vector<int> temp;
        temp.push_back(intervals[0][0]);
        temp.push_back(intervals[0][1]);
        res.push_back(temp);
        for (int i = 1; i < intervals.size(); i++)
        {
            if (res[count][1] >= intervals[i][0])
            {
                if (res[count][1] <= intervals[i][1])
                {
                    res[count][1] = intervals[i][1];
                }
            }
            else
            {
                count++;
                temp[0] = intervals[i][0];
                temp[1] = intervals[i][1];
                res.push_back(temp);
            }
        }
        return res;
    }
};
```