原题链接
双指针题目,测试用例会爆 int,所以需要转成 long long 进行计算。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| class Solution { public: vector<string> summaryRanges(vector<int>& nums) { if(nums.size() == 0) return {}; vector<string> res; for(int i=0, j=1;i<nums.size();i++) { while(j<nums.size()&&(long long)nums[j] - (long long)nums[j-1] == 1) j++; if((long long)nums[i]!=(long long)nums[j-1]) res.push_back(to_string(nums[i]) + "->" + to_string(nums[j-1])); else res.push_back(to_string(nums[i])); i = j-1; j++; } return res; } };
|