C++ AC implementation


  • 0

    Here is the first AC version implementation by me.

    I use 2 pointers to record the ascending index.

    But in the first submissions, I can not AC as I do not deal with the ending cases.

    For example

        [1, 2, 3, 4, 7]
    

    It only returns

       ["1->4"]
    

    Why it misses the "7", because my while loop checking condition is not complete.

    To fix the bug, I have to deal with the ending condition specially.

    Here is my code :

    class Solution {
    public:
        vector<string> summaryRanges(vector<int>& nums) {
            int len=nums.size();
            vector<string> result;
            if(len==0)  return result;
            if(len==1)  { 
                result.push_back(to_string(nums[0])); 
                return result;
            }
            
            int start=0, cur=1, pre=0;
            while(cur<len){
                if(nums[cur]-nums[pre]==1) { 
                    cur++; 
                    pre++; 
                }
                else { 
                    if(pre>start) 
                        result.push_back(to_string(nums[start])+"->"+to_string(nums[pre]));
                    else result.push_back(to_string(nums[start]));
                    start=cur;
                    cur++;
                    pre++;
                }
                
                if(cur==len) {
                    if(pre>start) 
                        result.push_back(to_string(nums[start])+"->"+to_string(nums[pre]));
                    else result.push_back(to_string(nums[start]));
                }
            }
            return result;
        }
    };

  • 0

    Thanks the much more clearer post from @lchen77

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

  • 0

    The trick is just to use the "end+1<len" not "end<len"


  • 0

    The corner cases is that we should not miss when checking the while conditions.


  • 0
    2
    class Solution {
    public:
        vector<string> summaryRanges(vector<int>& nums) {
            int size_nums = nums.size();
            vector<string> result;
            if(size_nums == 0)  return result;
            if(size_nums == 1)  return vector<string> {to_string(nums[0])};
            
            int start = nums[0], cur = 1, end = nums[0];
            nums.push_back(INT_MAX);
            while(cur <= size_nums) {
                if(nums[cur]-nums[cur-1] == 1) {
                    cur ++;
                }
                else {
                    if(nums[cur-1] > start) {
                        result.push_back(to_string(start) + "->" + to_string(nums[cur-1]));
                    }
                    else {
                        result.push_back(to_string(start));
                    }
                    start = nums[cur];
                    cur++;
                }
            }
            return result;
        }
    };

Log in to reply
 

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