LeetCode 1285: Find the Start and End Number of Continuous Ranges Solution

Master LeetCode problem 1285 (Find the Start and End Number of Continuous Ranges), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1285. Find the Start and End Number of Continuous Ranges

Medium

Problem Explanation

Explanation:

Given an array of integers, we need to find all continuous ranges in the array and return the start and end number of each range.

To solve this problem, we can iterate through the array and keep track of the start and end of each continuous range. Whenever we encounter a break in the continuity, we add the current range to our result. Solution:

Solution Code

import java.util.ArrayList;
import java.util.List;

class Solution {
    public List<List<Integer>> findRanges(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        
        int start = nums[0];
        int end = nums[0];
        
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] == end + 1) {
                end = nums[i];
            } else {
                List<Integer> range = new ArrayList<>();
                range.add(start);
                range.add(end);
                result.add(range);
                
                start = nums[i];
                end = nums[i];
            }
        }
        
        List<Integer> range = new ArrayList<>();
        range.add(start);
        range.add(end);
        result.add(range);
        
        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1285 (Find the Start and End Number of Continuous Ranges)?

This page provides optimized solutions for LeetCode problem 1285 (Find the Start and End Number of Continuous Ranges) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1285 (Find the Start and End Number of Continuous Ranges)?

The time complexity for LeetCode 1285 (Find the Start and End Number of Continuous Ranges) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1285 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1285 in Java, C++, or Python.

Back to LeetCode Solutions