Given a sorted integer array without duplicates, return the summary of its ranges.
For example, given [0,1,2,4,5,7]
, return ["0->2","4->5","7"].
Runtime: 0ms
1 class Solution { 2 public: 3 vector<string> summaryRanges(vector<int>& nums) { 4 vector<string> result; 5 6 if(nums.empty()) return result; 7 int left = 0, right = left + 1; 8 while(left < nums.size()){ 9 int gap = 1; 10 while(right < nums.size() && nums[right] - nums[left] == gap){ 11 right++; 12 gap++; 13 } 14 result.push_back(left == right - 1 ? to_string(nums[left]) : 15 to_string(nums[left]) + "->" + to_string(nums[right - 1])); 16 left = right; 17 right = left + 1; 18 } 19 return result; 20 } 21 };