标签:
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> vec; 5 if(nums.empty()) 6 return vec; 7 8 int low=nums[0],high=nums[0]; 9 for(int i = 1; i < nums.size(); i ++) 10 { 11 if(nums[i]-nums[i-1] == 1)high=nums[i]; 12 else 13 { 14 string range; 15 if(low != high) 16 range = to_string(low) + "->" + to_string(high); 17 else 18 range = to_string(low); 19 vec.push_back(range); 20 low = nums[i]; 21 high = nums[i]; 22 } 23 } 24 string range; 25 if(low != high) 26 range = to_string(low) + "->" + to_string(high); 27 else 28 range = to_string(low); 29 vec.push_back(range); 30 return vec; 31 } 32 };
【LeetCode】228 - Summary Ranges
标签:
原文地址:http://www.cnblogs.com/irun/p/4687845.html