标签:
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"].
思想很简单 逐个比较前后两个数的差值。
1 class Solution { 2 public: 3 vector<string> summaryRanges(vector<int>& nums) { 4 vector<string> res ; 5 if(nums.size()<1 ) return res; 6 int i = 0; 7 int n = nums.size(); 8 while(i<n) 9 { 10 int j = 1; 11 while(i+j<n && nums[i] == nums[i+j]-j) ++j; 12 res.push_back(j<=1?to_string(nums[i]):to_string(nums[i])+"->"+to_string(nums[i+j-1])); 13 i = i + j; 14 } 15 return res; 16 } 17 };
标签:
原文地址:http://www.cnblogs.com/chdxiaoming/p/4701496.html