Python | Leetcode Python题解之第386题字典序排数

题目:

Python | Leetcode Python题解之第386题字典序排数_第1张图片

题解:

class Solution:
    def lexicalOrder(self, n: int) -> List[int]:
        ans = [0] * n
        num = 1
        for i in range(n):
            ans[i] = num
            if num * 10 <= n:
                num *= 10
            else:
                while num % 10 == 9 or num + 1 > n:
                    num //= 10
                num += 1
        return ans

你可能感兴趣的:(分享,Python,Leetcode,题解)