Created
May 27, 2024 09:39
-
-
Save shitu13/7f5a732be153aafc2c3650ba9481472b to your computer and use it in GitHub Desktop.
Summary Ranges
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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