코테준비 - Summary Ranges

정상화·2023년 2월 26일

LeetCode

목록 보기
198/222

Summary Ranges

class Solution {
public:
  vector<string> summaryRanges(vector<int> &nums) {
      if (nums.empty()) {
          return {};
      }
      vector<vector<int>> consecutives;
      consecutives.push_back({nums.front()});

      for (auto it = nums.begin() + 1; it != nums.end(); it++) {
          int prev = consecutives.back().back();
          if (prev + 1 == *it) {
              consecutives.back().push_back(*it);
          } else {
              consecutives.push_back({*it});
          }
      }

      vector<string> res;
      for (auto &consecutive: consecutives) {
          if (consecutive.size() == 1) {
              res.push_back(to_string(consecutive.front()));
          } else {
              string exp = to_string(consecutive.front())
                           + "->"
                           + to_string(consecutive.back());
              res.push_back(exp);
          }
      }
      return res;
  }
};
profile
백엔드 희망

0개의 댓글