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