[LeetCode] 5081. step number

Portal: [LeetCode] 5081. step number

Title Description

If each digit on an integer number on its position adjacent absolute difference is 1, this number is a "number of steps."

For example, 321a number of steps, rather 421than.

Give you two integers, lowand high, you find out [low, high]all the number of steps in the range, and return the sorted results.

Example:

Input: low = 0, = 21 high
output: [0,1,2,3,4,5,6,7,8,9,10,12,21]

prompt:

  • 0 <= low <= high <= 2 * 10^9

Analysis and Code

  • A start request is contemplated that first of all the number of steps out into a collection, then traverse whichever >=low && <= highnumber is added to the result set Result, arranged for the final sequence.
  • Seeking a number of steps of the DFS method, beginning 1-9, by 10 each time the current number digit plus 1 plus or minus 1. Special note of bits 0 and 9 just fine.
  • 0 special treatment.

Code

class Solution {
    public List<Integer> countSteppingNumbers(int low, int high) {
        List<Integer> list = new ArrayList<>();
        list.add(0);
        for (int i = 1; i <= 9; i++) {
            dfs(list, i);
        }
        List<Integer> result = new ArrayList<>();
        for (int num : list) {
            if (num >= low && num <= high) {
                result.add(num);
            }
        }
        Collections.sort(result);
        return result;
    }

    public void dfs(List<Integer> list, int cur) {
        list.add(cur);
        if (cur > Integer.MAX_VALUE / 10) {
            return;
        }
        int r = cur % 10;
        if (r != 9) {
            dfs(list, cur * 10 + r + 1);
        }
        if (r != 0) {
            dfs(list, cur * 10 + r - 1);
        }
    }
}

A solution, DFS

  • All the results may not be obtained only in less than recursive high range, add only results meet the requirements of the DFS process.

Code:

class Solution {
    public List<Integer> countSteppingNumbers(int low, int high) {
        List<Integer> result = new ArrayList<>();
        if (low == 0) {
            result.add(0);
        }
        for (int i = 1; i <= 9; i++) {
            dfs(result, i, low, high);
        }
        Collections.sort(result);
        return result;
    }

    public void dfs(List<Integer> result, int cur, int low, int high) {
        if (cur >= low && cur <= high) {
            result.add(cur);
        }
        if (cur > high / 10) {
            return;
        }
        int r = cur % 10;
        if (r != 9 && cur * 10 + r + 1 <= high) {
            dfs(result, cur * 10 + r + 1, low, high);
        }
        if (r != 0 && cur * 10 + r - 1 <= high) {
            dfs(result, cur * 10 + r - 1, low, high);
        }
    }
}

Solution two, BFS

  • 1-9 first added to the queue, the queue number is added to the result set result is then taken out each time, to meet the requirements, and the number of steps within a range of the added queue.

Code:

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

summary

1-9 is the beginning, the number of steps is determined, the DFS may be used, can also be used BFS.


Guess you like

Origin www.cnblogs.com/qiu_jiaqi/p/LeetCode_5081.html