C++ code using set


  • 0
    M
    class Solution {
    public:
        vector<string> summaryRanges(vector<int>& nums) {
            set<int> result(nums.begin(), nums.end());
            int value = 0;
            int start, end;
            vector<string> res;
            while(!result.empty())
            {
                value = *result.begin();
                result.erase(value);
                start = value;
                for(int i = value + 1; result.find(i) != result.end(); i++)
                {
                    result.erase(i);
                    start++;
                }
                if(start == value)
                res.push_back(to_string(value));
                else
                res.push_back(to_string(value) + "->" + to_string(start));
            }
            return res;
            
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.