304 North Cardinal St.
Dorchester Center, MA 02124

# Summary Ranges LeetCode Solution

## Problem – Summary Ranges LeetCode Solution

You are given a sorted unique integer array `nums`.

range `[a,b]` is the set of all integers from `a` to `b` (inclusive).

Return the smallest sorted list of ranges that cover all the numbers in the array exactly. That is, each element of `nums` is covered by exactly one of the ranges, and there is no integer `x` such that `x` is in one of the ranges but not in `nums`.

Each range `[a,b]` in the list should be output as:

• `"a->b"` if `a != b`
• `"a"` if `a == b`

Example 1:

``````Input: nums = [0,1,2,4,5,7]
Output: ["0->2","4->5","7"]
Explanation: The ranges are:
[0,2] --> "0->2"
[4,5] --> "4->5"
[7,7] --> "7"
``````

Example 2:

``````Input: nums = [0,2,3,4,6,8,9]
Output: ["0","2->4","6","8->9"]
Explanation: The ranges are:
[0,0] --> "0"
[2,4] --> "2->4"
[6,6] --> "6"
[8,9] --> "8->9"
``````

Constraints:

• `0 <= nums.length <= 20`
• `-231 <= nums[i] <= 231 - 1`
• All the values of `nums` are unique.
• `nums` is sorted in ascending order.

## Summary Ranges LeetCode Solution in Java

``````List<String> list=new ArrayList();
if(nums.length==1){
return list;
}
for(int i=0;i<nums.length;i++){
int a=nums[i];
while(i+1<nums.length&&(nums[i+1]-nums[i])==1){
i++;
}
if(a!=nums[i]){
}else{
}
}
return list;
``````

## Summary Ranges LeetCode Solution in C++

``````   vector<string> summaryRanges(vector<int>& nums) {
const int size_n = nums.size();
vector<string> res;
if ( 0 == size_n) return res;
for (int i = 0; i < size_n;) {
int start = i, end = i;
while (end + 1 < size_n && nums[end+1] == nums[end] + 1) end++;
if (end > start) res.push_back(to_string(nums[start]) + "->" + to_string(nums[end]));
else res.push_back(to_string(nums[start]));
i = end+1;
}
return res;
}
``````

## Summary Ranges LeetCode Solution in Python

``````def summaryRanges(self, nums):
ranges = []
for n in nums:
if not ranges or n > ranges[-1][-1] + 1:
ranges += [],
ranges[-1][1:] = n,
return ['->'.join(map(str, r)) for r in ranges]
``````
##### Summary Ranges LeetCode Solution Review:

In our experience, we suggest you solve this Summary Ranges LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Summary Ranges LeetCode Solution

Find on Leetcode

##### Conclusion:

I hope this Summary Ranges LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions