C++ 0ms easy to undersatnd


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

Log in to reply
 

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