C plus plus implementation zero ms


  • 0
    H
    class Solution {
    public:
        vector<string> summaryRanges(vector<int>& nums) {
            vector<string> ranges;
            int ptr1 = 0;
            int ptr2 = 1;
            while(ptr2 < nums.size()+1) {
                if(ptr2 < nums.size() && nums[ptr2]-nums[ptr2-1] == 1) {
                    ptr2++;
                }
                else {
                    char tmp[25];
                    sprintf(tmp, "%d", nums[ptr1]);
                    string st(tmp);
                    sprintf(tmp, "%d", nums[ptr2-1]);
                    string ed(tmp);
                    if(st == ed) {
                        ranges.push_back(st);
                    }
                    else {
                        ranges.push_back(st+"->"+ed);
                    }
                    ptr1 = ptr2;
                    ptr2++;
                }
            }
            
            return ranges;
        }
    };

Log in to reply
 

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