My c++ solution


  • 0
    M
    class Solution {
    public:
        vector<string> summaryRanges(vector<int>& nums) {
            vector<string> ret;
            ret.clear();
            int x = 0;
            if(nums.size() == 0) return ret;
            for(int i = 0; i < nums.size(); i++) {
                if(i > 0 && nums[i] != nums[i - 1] + 1) {
                    if(x == i - 1)
                        ret.push_back(to_string(nums[x]));
                    else
                        ret.push_back(to_string(nums[x]) + "->" + to_string(nums[i - 1]));
                    x = i;
                }
            }
            if(x == nums.size() - 1)
                ret.push_back(to_string(nums[x]));
            else
                ret.push_back(to_string(nums[x]) + "->" + to_string(nums[nums.size() - 1]));
            return ret;
        }
    };

Log in to reply
 

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