My easy simple c++ solution


  • 0
    S
    vector<string> summaryRanges(vector<int>& nums) {
            vector<string> result;
            
            if (nums.size() == 0) return result;
            
            int from = 0;
    
            for (int i = 1; i <= nums.size(); i++) {
                if (i == nums.size() || nums[i-1] + 1 != nums[i]) {
                    string str;
                    if (i-1 == from) {
                        str.append(to_string(nums[from]));
                    }
                    else {
                        str.append (to_string(nums[from]));
                        str.append ("->");
                        str.append (to_string(nums[i-1]));
                    }
                    result.push_back(str);
                    from = i;
                }
            }
            return result;
        }

Log in to reply
 

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