[Recursion] [leetcode] order times

topic:

We define the "order of order" as: the number on each digit is an integer that is 1 greater than the number on the previous digit.

Please return to the ordered list (sorted from smallest to largest) consisting of all the order numbers in the range of [low, high].

Example 1:

Output: low = 100, high = 300
Output: [123,234]
Example 2:

Output: low = 1000, high = 13000
Output: [1234,2345,3456,4567,5678,6789,12345]

source:

1291. Straight times

Problem-solving idea 1: Recursion

Get this question, the first reaction is recursion, take a look at the backtracking code. You may ask: why not call backtracking? In this question, there is only one possibility in the recursive function. For example, the current number is 12, then the next number must be 123, not 124, so there is no backtracking.

Points to note when using recursion:

  • The result is out of order and needs to be output after sorting
  • Pay attention to the treatment when the number reaches 10
class Solution {
public:
    vector<int> result;
    vector<int> sequentialDigits(int low, int high) {
        for (int i = 1; i < 10; i++) {
            int path = i;
            back(path, i+1, low, high);
        }
        sort(result.begin(), result.end());
        return result;
    }
    void back(int path, int next, int low, int high) {
        if (path >= low) {
            result.push_back(path);
        }
        if (next == 10) return;
        path = path * 10 + next;
        if (path <= high) {
            back(path, next+1, low, high);
        }
    }
};

Problem-solving idea 2: A clever idea

Define all the sequence times in advance, just traverse, and the code is simple without explanation.

class Solution {
public:
    vector<int> sequentialDigits(int low, int high) {
        vector<int> result;
        int nums[36] = {
            12,23,34,45,56,67,78,89,
            123,234,345,456,567,678,789,
            1234,2345,3456,4567,5678,6789,
            12345,23456,34567,45678,56789,
            123456,234567,345678,456789,
            1234567,2345678,3456789,
            12345678,23456789,
            123456789};
        for (int i = 0; i < 36; i++) {
            if (nums[i] >= low && nums[i] <= high) {
                result.push_back(nums[i]);
            }
        }
        return result;
    }
};

Problem-solving idea 3: Law

According to idea 2, nums starts from the first line, and the number width is 2, 3, 4, 5, 6, 7, 8, 9. Define a variable width to record the width of the number, which can start from 2 or the width of the number low.

Under the same width, slide the number window to get the next number based on the current number. How to calculate it, the following example illustrates:

  • In the case of width=3, if the current number is 234, then the next number is: (2345)% 1000 = 345.
  • In the case of width=5, if the current number is 23456, then the next number is: (234567)% 100000 = 34567.

Define a variable mod to record the above 1000, 100000, whenever width+1, mod = mod*10.

If the current number>high, the traversal ends.

class Solution {
public:
    vector<int> sequentialDigits(int low, int high) {
        vector<int> result;
        int width = 0; // 位数
        int t = low;
        long mod = 1;
        while (t > 0) {
            width++;
            t /= 10;
            mod *= 10;
        }
        
        while (width < 10) {
            bool end = false;
            long sum = 0; // width组成的数字
            int n = 1;
            for (; n < width; n++) sum = sum * 10 + n; // 前width-1组成的数字
            for (; n < 10; n++) {
                sum = (sum * 10 + n) % mod;
                if (sum > high) {
                    end = true;
                    break;
                }
                if (sum >= low) {
                    result.push_back(sum);
                }
            }
            if (end) break;
            width++;
            mod *= 10;
        }
        return result;
    }
};

to sum up:

I prefer ideas 1 and 2. Needless to say, idea 2 can be written according to the retrospective routine, which is easier to implement.

Idea 3 has no routines to follow, and it needs to be debugged repeatedly to pass. Maybe it runs more efficiently, but it is complicated to implement and time-consuming to write, so it is not recommended.

Guess you like

Origin blog.csdn.net/hbuxiaoshe/article/details/115244698