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

Source: force buckle( LeetCode)
Link: https://leetcode-cn.com/problems/summary-ranges
 The copyright belongs to Lingkou network. For commercial reprint, please contact the official authorization, and for non-commercial reprint, please indicate the source.

Example 2:

Input: nums = [0,2,3,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 = []

Example 4:

Input: nums = [-1]

Example 5:

Input: nums = [0]


  • 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


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

            //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){
                if(i==j) res.Add($"{nums[i]}");
                else res.Add($"{nums[i]}->{nums[j]}");
                i = j+1;
        return res;

results of enforcement

 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 '.


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;
            while (i < n && nums[i] == nums[i - 1] + 1) {
            int high = i - 1;
            StringBuffer temp = new StringBuffer(Integer.toString(nums[low]));
            if (low < high) {
        return ret;

results of enforcement

 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

Tags: Java Algorithm leetcode

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