# [small Y learning algorithm] β‘ Daily LeetCode punch in β‘ Table - 49. Summary interval

#### π’ preface

 π Algorithm problem π
• π² Punching out an algorithm problem every day is not only a learning process, but also a sharing process π
• π² Tip: the problem-solving programming languages in this column are C# and Java
• π² To maintain a state of learning every day, let's work together to become the great God of algorithm π§!
• π² Today is the 49th day of punching out the force deduction algorithm π!
 π Algorithm problem π

## π² Example of original question: summary interval

Given an ordered integer array nums without duplicate elements.

Returns a list of the smallest ordered interval ranges that exactly cover all numbers in the array. That is, each element of num is exactly covered by an interval range, and there is no number x belonging to a range but not num.

Each interval range [a,b] in the list shall be output in the following format:

• "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 range is:
[0,2] --> "0->2"
[4,5] --> "4->5"
[7,7] --> "7"

Source: force buckle( LeetCode)
```

Example 2:

```Input: nums = [0,2,3,4,6,8,9]
Output:["0","2->4","6","8->9"]
Explanation: the range is:
[0,0] --> "0"
[2,4] --> "2->4"
[6,6] --> "6"
[8,9] --> "8->9"

```

Example 3:

```Input: nums = []
Output:[]
```

Example 4:

```Input: nums = [-1]
Output:["-1"]
```

Example 5:

```Input: nums = [0]
Output:["0"]
```

Tips:

• 0 <= nums.length <= 20
• -231 <= nums[i] <= 231 - 1
• All values in nums are different from each other
• nums in ascending order

### π» C# method: depth first search

1. In essence, it is a circular array to judge whether the difference between the current position and the previous position (or between the current position and the next position) is 1. If yes, continue, and if not, add it to the list
2. There are also two cases of adding to the list.
a. If the continuous value exceeds 1, use X - > y format
b. Continuous values equal to 1 and no more than 1 are in x format

code:

```public class Solution {
public IList<string> SummaryRanges(int[] nums) {
List<string> res = new List<string>();
int i=0,j=0;

while(j<nums.Length){
//J = = num.length-1 is an important judgment, and the last situation of the array can be considered
if(j==nums.Length-1 || nums[j+1]-nums[j]!=1){
i = j+1;
}
++j;
}
return res;
}
}
```

results of enforcement

```adopt
Execution time: 232 msοΌAt all C# Defeated 69.77% of users in submission
Memory consumption: 30 MBοΌAt all C# Defeated 74.42% of users in submission
```

### π» Java method: one-time traversal

Train of thought analysis

We start from the position 00 of the array and traverse to the right. Every time we encounter that the difference between adjacent elements is greater than 11, we find an interval. After traversing the array, we can get a list of a series of intervals.

In the traversal process, maintain the subscripts low and high to record the starting point and ending point of the interval respectively. For any interval, there is low ≤ high. When an interval is obtained, the string representation of the interval is generated according to the values of low and high.

• When low < high, the string of the interval is expressed as' low → high ';
• When low=high, the string of the interval is represented as' low '.

code:

```class Solution {
public List<String> summaryRanges(int[] nums) {
List<String> ret = new ArrayList<String>();
int i = 0;
int n = nums.length;
while (i < n) {
int low = i;
i++;
while (i < n && nums[i] == nums[i - 1] + 1) {
i++;
}
int high = i - 1;
StringBuffer temp = new StringBuffer(Integer.toString(nums[low]));
if (low < high) {
temp.append("->");
temp.append(Integer.toString(nums[high]));
}
}
return ret;
}
}
```

results of enforcement

```adopt
Execution time: 0 msοΌAt all Java  Defeated 100 in submission.00%User
Memory consumption: 36.5 MBοΌAt all Java Beat 65 in submission.23%User
```

Complexity analysis

```Time complexity: O( n )
Space complexity: O( 1 )
```

## π¬ summary

• Today is the 49th day of punching out the force deduction algorithm!
• This paper uses C# and Java programming languages to solve problems
• Some methods are also written by Likou God, and they are also shared while learning. Thanks again to the algorithm bosses
• That's the end of today's algorithm sharing. See you tomorrow!

#### π Previous high-quality article sharing

Posted on Mon, 04 Oct 2021 14:21:57 -0400 by FredAt