順序付けられた整数配列numsnumsが与えられ、要素が繰り返されていないN U M S。
配列内のすべての数値をカバーする最小の順序付き範囲範囲のリストを返します。言い換えれば、nums numsn u m sの各要素は特定の範囲で正確にカバーされており、特定の範囲に属するnumsnumsはありません。N U Mの番号XXx。
リスト内の各間隔範囲[a、b] [a、b][ 、b ]は次の形式で出力する必要があります。
"a->b"
もしa != b
"a"
、もしa == b
例1:
输入:nums = [0,1,2,4,5,7]
输出:["0->2","4->5","7"]
解释:区间范围是:
[0,2] --> "0->2"
[4,5] --> "4->5"
[7,7] --> "7"
例2:
输入:nums = [0,2,3,4,6,8,9]
输出:["0","2->4","6","8->9"]
解释:区间范围是:
[0,0] --> "0"
[2,4] --> "2->4"
[6,6] --> "6"
[8,9] --> "8->9"
例3:
输入:nums = []
输出:[]
例4:
输入:nums = [-1]
输出:["-1"]
例5:
输入:nums = [0]
输出:["0"]
促す:
0 <= nums。長さ<= 20 0 <= nums.length <= 200<=N U M S 。l e n g t h<=2 0
− 2 3 1 <= nums [i] <= 2 31 − 1 -2 ^ 31 <= nums [i] <= 2 ^ {31} -1− 23 1<=n u m s [ i ]<=23 1−1
nums numsすべての値N Uはmはsが互いに異なる
NUMSのNUMSN U M S昇順
class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
vector<string>res;
int i=0;
int n=nums.size();
while (i<n){
int l=i;
i++;
while (i<n&&nums[i]==nums[i-1]+1){
i++;
}
int r=i-1;
string tmp=to_string(nums[l]);
if (l<r){
tmp+="->";
tmp+=to_string(nums[r]);
}
res.push_back(tmp);
}
return res;
}
};