leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0228.cpp (708B)


      1 class Solution {
      2   public:
      3     vector<string> summaryRanges(vector<int> &nums) {
      4         if (!nums.size()) return {};
      5         vector<string> res;
      6         int start = 0;
      7         for (int i = 1; i < nums.size(); i++) {
      8             if (nums[i] == nums[i - 1] + 1) continue;
      9             if (i - start > 1)
     10                 res.push_back(to_string(nums[start]) + "->" + to_string(nums[i - 1]));
     11             else
     12                 res.push_back(to_string(nums[start]));
     13             start = i;
     14         }
     15         if (nums.size() - start > 1)
     16             res.push_back(to_string(nums[start]) + "->" + to_string(nums.back()));
     17         else
     18             res.push_back(to_string(nums[start]));
     19 
     20         return res;
     21     }
     22 };