My 13 line c++ code


  • 1
    S
       // author : s2003zy
       // weibo  : weibo.com/songzy982
       // blog : s2003zy.com
       // mail : s2003zy#gmail.com
        class Solution {
        public:
            vector<string> summaryRanges(vector<int>& nums) {
                vector<string> res;
                nums.push_back(INT_MIN);
                long long start = nums[0], last = nums[0];
                for (int i = 1; i < nums.size(); i++) {
                    if (last + 1 != (long long)nums[i]) {
                        res.push_back(to_string(start));
                        if (last != start) {
                            res[res.size() - 1] = res[res.size() - 1] + "->" + to_string(last);
                        }
                        start = nums[i];
                    }
                    last = nums[i];
                }
                return res;
            }
        };

Log in to reply
 

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