LeetCode 1215: Stepping Numbers Solution

Master LeetCode problem 1215 (Stepping Numbers), 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.

1215. Stepping Numbers

Problem Explanation

Explanation:

Given a range [low, high], we need to find all stepping numbers in that range. A stepping number is a number where each digit differs by 1 from the adjacent digits.

To solve this problem, we will perform a Breadth First Search (BFS) starting from the single-digit numbers (1 to 9) and expanding to the next possible stepping numbers in a level-wise manner. We will keep track of all valid stepping numbers within the given range.

Algorithm:

  1. Initialize a queue with all single-digit numbers from 0 to 9.
  2. While the queue is not empty, pop a number from the queue.
  3. If the number is within the range [low, high], add it to the result.
  4. For each valid stepping number, i.e., numbers where the last digit is at most 1 away from the current number:
    • If the new number is less than or equal to high, add it to the queue to explore further.
  5. Repeat steps 2-4 until the queue is empty.
  6. Return the list of stepping numbers within the range [low, high].

Time Complexity:

  • The time complexity of this algorithm is O(n), where n is the total number of stepping numbers within the given range.

Space Complexity:

  • The space complexity of this algorithm is O(k), where k is the total number of stepping numbers within the given range.

:

Solution Code

class Solution {
    public List<Integer> countSteppingNumbers(int low, int high) {
        List<Integer> result = new ArrayList<>();
        Queue<Integer> queue = new LinkedList<>();
        
        if (low == 0) {
            result.add(0);
        }
        
        for (int i = 1; i <= 9; i++) {
            queue.offer(i);
        }
        
        while (!queue.isEmpty()) {
            int curr = queue.poll();
            if (curr >= low && curr <= high) {
                result.add(curr);
            }
            int lastDigit = curr % 10;
            
            if (lastDigit > 0) {
                int nextNum = curr * 10 + (lastDigit - 1);
                if (nextNum <= high) {
                    queue.offer(nextNum);
                }
            }
            
            if (lastDigit < 9) {
                int nextNum = curr * 10 + (lastDigit + 1);
                if (nextNum <= high) {
                    queue.offer(nextNum);
                }
            }
        }
        
        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1215 (Stepping Numbers)?

This page provides optimized solutions for LeetCode problem 1215 (Stepping Numbers) 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 1215 (Stepping Numbers)?

The time complexity for LeetCode 1215 (Stepping Numbers) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1215 on DevExCode?

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

Back to LeetCode Solutions