Skip to content

Commit e0585b4

Browse files
committed
Summary Ranges
1 parent 9852af2 commit e0585b4

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
+ [217 Contains Duplicate(hashset)](algorithms/ContainsDuplicate)
126126
+ [219 Contains Duplicate II(hashmap)](algorithms/ContainsDuplicateII)
127127
+ [226 Invert Binary Tree(递归,树)](algorithms/InvertBinaryTree)
128+
+ [228 Summary Ranges](algorithms/SummaryRanges)
128129
+ [231 Power of Two(位运算,二进制1的个数)](algorithms/PowerofTwo)
129130

130131
## Database

algorithms/SummaryRanges/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
## Summary Ranges
2+
3+
Given a sorted integer array without duplicates, return the summary of its ranges.
4+
5+
For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"].
6+
7+
Credits:
8+
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
9+
10+
## Solution
11+
12+
直接遍历数组即可
13+
14+
```cpp
15+
vector<string> summaryRanges(vector<int> &nums) {
16+
vector<string> result;
17+
int n = nums.size();
18+
if (n == 0) {
19+
return result;
20+
}
21+
int s = nums[0], t;
22+
bool update = false;
23+
int i;
24+
for (i = 1; i < n; ++i) {
25+
if (nums[i] == nums[i - 1] + 1) {
26+
update = true;
27+
t = nums[i];
28+
} else {
29+
if (update) {
30+
result.push_back(to_string(s) + "->" + to_string(t));
31+
s = nums[i];
32+
update = false;
33+
} else {
34+
result.push_back(to_string(s));
35+
s = nums[i];
36+
}
37+
}
38+
}
39+
// last update
40+
if (update) {
41+
result.push_back(to_string(s) + "->" + to_string(nums[n - 1]));
42+
} else {
43+
result.push_back(to_string(nums[n - 1]));
44+
}
45+
return result;
46+
}
47+
```

algorithms/SummaryRanges/solve.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <string>
2+
#include <vector>
3+
#include <iostream>
4+
#include <algorithm>
5+
#include <cstdio>
6+
using namespace std;
7+
class Solution {
8+
public:
9+
vector<string> summaryRanges(vector<int> &nums) {
10+
vector<string> result;
11+
int n = nums.size();
12+
if (n == 0) {
13+
return result;
14+
}
15+
int s = nums[0], t;
16+
bool update = false;
17+
int i;
18+
for (i = 1; i < n; ++i) {
19+
if (nums[i] == nums[i - 1] + 1) {
20+
update = true;
21+
t = nums[i];
22+
} else {
23+
if (update) {
24+
result.push_back(to_string(s) + "->" + to_string(t));
25+
s = nums[i];
26+
update = false;
27+
} else {
28+
result.push_back(to_string(s));
29+
s = nums[i];
30+
}
31+
}
32+
}
33+
if (update) {
34+
result.push_back(to_string(s) + "->" + to_string(nums[n - 1]));
35+
} else {
36+
result.push_back(to_string(nums[n - 1]));
37+
}
38+
return result;
39+
}
40+
};
41+
int main(int argc, char **argv)
42+
{
43+
Solution solution;
44+
vector<int> nums = {0,5,9};
45+
auto result = solution.summaryRanges(nums);
46+
for_each(begin(result), end(result), [](string s) {cout << s << endl;});
47+
return 0;
48+
}

0 commit comments

Comments
 (0)