← Home

228. Summary Ranges

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
228

I like to read this solution as a small machine: keep the useful information, throw away the noise. For 228. Summary Ranges, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

Guide

When?

Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are summaryRanges.

Guide

Why?

The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.

  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Initialize the memory or helper structure.
  2. Process candidates in the order the invariant expects.
  3. Update the answer only when the current state is valid.
  4. Return the value that represents the fully processed input.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Summary Ranges.
02//Memory Usage: 7 MB, less than 100.00% of C++ online submissions for Summary Ranges.
03class Solution {
04public:
05    vector<string> summaryRanges(vector<int>& nums) {
06        int n = nums.size();
07        if(n == 0) return vector<string>();
08        int start = nums[0], end = start;
09        vector<string> ans;
10        for(int i = 1; i < n; i++){
11            // if(nums[i] - end == 1){
12            if(end != INT_MAX && nums[i] == end+1){ //avoid overflow
13                //extend old range
14                end = nums[i];
15            }else{
16                //new range
17                if(start == end)
18                    ans.push_back(to_string(start));
19                else
20                    ans.push_back(to_string(start) + "->" + to_string(end));
21                start = end = nums[i];
22            }
23        }
24        
25        //handle the last interval and when n is equal to 1
26        if(start == end)
27            ans.push_back(to_string(start));
28        else
29            ans.push_back(to_string(start) + "->" + to_string(end));
30        
31        return ans;
32    }
33};
34
35//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Summary Ranges.
36//Memory Usage: 6.8 MB, less than 100.00% of C++ online submissions for Summary Ranges.
37//https://leetcode.com/problems/summary-ranges/discuss/63219/Accepted-JAVA-solution-easy-to-understand
38class Solution {
39public:
40    vector<string> summaryRanges(vector<int>& nums) {
41        int n = nums.size();
42        if(n == 0) return vector<string>();
43        int start;
44        vector<string> ans;
45        for(int i = 0; i < n; i++){
46            start = nums[i];
47            while(i+1 < n && nums[i] != INT_MAX && nums[i+1] == nums[i]+1){
48                i++;
49            }
50            //now i is the index of the end of the interval
51            
52            if(start == nums[i])
53                ans.push_back(to_string(start));
54            else
55                ans.push_back(to_string(start) + "->" + to_string(nums[i]));
56        }
57        
58        return ans;
59    }
60};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.