Skip to content

Instantly share code, notes, and snippets.

@shitu13
Created May 27, 2024 09:39
Show Gist options
  • Save shitu13/7f5a732be153aafc2c3650ba9481472b to your computer and use it in GitHub Desktop.
Save shitu13/7f5a732be153aafc2c3650ba9481472b to your computer and use it in GitHub Desktop.
Summary Ranges
class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
int n = nums.size();
vector<string> res;
for (int i = 0; i < n; i++) {
int start = nums[i];
while (i+1 < n && nums[i+1] == nums[i] + 1) {
i++;
}
if (start != nums[i])
res.push_back(to_string(start) + "->" + to_string(nums[i]));
else
res.push_back(to_string(start));
}
return res;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment