C++ standard 2-pointer solution


  • 1
    D
    class Solution {
    public:
        vector<string> summaryRanges(vector<int>& nums) {
            vector<string> result;
            int left = 0, right = 0, n = nums.size();
            while(left < n) {
                right = left + 1;
                while(right < n && nums[right] - nums[left] == right - left) {
                    right++;
                }
                if(right > left + 1) result.push_back(to_string(nums[left]) + "->" + to_string(nums[right - 1]));
                else result.push_back(to_string(nums[left]));
                left = right;
            }
            return result;
        }
    };
    

  • 0
    Z

    @dr.pro YOu are talented!


Log in to reply
 

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