Summary Ranges Solutions in C++
Number 228
Difficulty Medium
Acceptance 39.6%
Link LeetCode
Other languages —
Solutions
C++ solution by haoel/leetcode
// Source : https://leetcode.com/problems/summary-ranges/// Author : Hao Chen// Date : 2015-07-03class Solution {public:string makeRange(int start, int end) {ostringstream oss;if (start != end) {oss << start << "->" << end;} else {oss << start;}return oss.str();}vector<string> summaryRanges(vector<int>& nums) {vector<string> result;int len = nums.size();if (len == 0) return result;// we have two pointer for range-starter and range-enderint start=nums[0], end=nums[0];for (int i=1; i<len; i++) {// if it is continous number, move the end pointer;if (nums[i] == end + 1) {end = nums[i];continue;}//if the number is not continous, push the range into result//and reset the start and end pointerresult.push_back(makeRange(start, end));start = end = nums[i];}//for the last rangeresult.push_back(makeRange(start, end));return result;}};