-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
SummaryRanges.cpp
55 lines (45 loc) · 1.62 KB
/
SummaryRanges.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Source : https://leetcode.com/problems/summary-ranges/
// Author : Hao Chen
// Date : 2015-07-03
/**********************************************************************************
*
* 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"].
*
* Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
*
**********************************************************************************/
class 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-ender
int 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 pointer
result.push_back(makeRange(start, end));
start = end = nums[i];
}
//for the last range
result.push_back(makeRange(start, end));
return result;
}
};