1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| class Solution { public: vector<string> summaryRanges(vector<int>& nums) { vector<string> ans; if (nums.size()) { int start = nums[0]; for (int i = 1; i < nums.size(); ++i) { if (nums[i] != nums[i - 1] + 1) { if (start == nums[i - 1]) ans.push_back(to_string(start)); else ans.push_back(to_string(start) + "->" + to_string(nums[i - 1])); start = nums[i]; } } if (start == nums.back()) ans.push_back(to_string(nums.back())); else ans.push_back(to_string(start) + "->" + to_string(nums.back())); } return ans; } };
|