Leetcode: 【每日一题】- 2019-12-24 1291. 顺次数

Created on 24 Dec 2019  ·  3Comments  ·  Source: azl397985856/leetcode

我们定义「顺次数」为:每一位上的数字都比前一位上的数字大 1 的整数。

请你返回由 [low, high] 范围内所有顺次数组成的 有序 列表(从小到大排序)。

 

示例 1:

输出:low = 100, high = 300
输出:[123,234]
示例 2:

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

提示:

10 <= low <= high <= 10^9

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/sequential-digits
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

Backtrack Daily Question Medium Slide Window stale

Most helpful comment

Java题解:打表

    public List<Integer> sequentialDigits(int low, int high) {

        int[] nums = new int[]{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};
        List<Integer> res = new ArrayList<>();
        for (int num : nums)
            if (num >= low && num <= high)
                res.add(num);

        return res;
    }

All 3 comments

Easy to understand Python Solution with slide window

  1. computed all possible sequentialDigits, put into a list ins (no more than 64)
  2. iterate ins, put satisfied number which is x >= low and x <= high into returned list
class Solution:
    def sequentialDigits(self, low: int, high: int) -> List[int]:
        numbers = "123456789"
        ins = []
        n = len(numbers)
        for length in range(1, n):
            for i in range(n - length):
                ins.append(int(numbers[i:i + length + 1]))
        return [x for x in ins if x >= low and x <= high]

Java题解:打表

    public List<Integer> sequentialDigits(int low, int high) {

        int[] nums = new int[]{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};
        List<Integer> res = new ArrayList<>();
        for (int num : nums)
            if (num >= low && num <= high)
                res.add(num);

        return res;
    }

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

azl397985856 picture azl397985856  ·  5Comments

azl397985856 picture azl397985856  ·  4Comments

azl397985856 picture azl397985856  ·  4Comments

azl397985856 picture azl397985856  ·  4Comments

azl397985856 picture azl397985856  ·  3Comments