題目連結

小心實作。

AC Code

 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
#ifdef LOCAL
#include <bits/stdc++.h>
using namespace std;

// tree node stuff here...
#endif

static int __initialSetup = []()
{
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    return 0;
}
();

class Solution
{
public:
    vector<string> summaryRanges(vector<int> &nums)
    {
        vector<string> ans;
        if ((int)nums.size() == 0)
            return ans;
        else if ((int)nums.size() == 1) {
            ans.push_back(to_string(nums[0]));
            return ans;
        }

        int l = nums[0], r = nums.back();
        for (int i = 1; i < (int)nums.size(); i++) {
            if (nums[i - 1] + 1 == nums[i]) {
                continue;
            } else {
                r = nums[i - 1];

                if (l == r)
                    ans.push_back(to_string(l));
                else
                    ans.push_back(to_string(l) + "->" + to_string(r));

                l = nums[i];
                r = nums.back();
            }
        }

        if (l == r)
            ans.push_back(to_string(l));
        else
            ans.push_back(to_string(l) + "->" + to_string(r));

        return ans;
    }
};

#ifdef LOCAL
int main()
{
    return 0;
}
#endif