0
点赞
收藏
分享

微信扫一扫

Python | Leetcode Python题解之第440题字典序的第K小数字

玉新行者 2024-09-27 阅读 15

题目:

题解:

class Solution:
    def getSteps(self, cur: int, n: int) -> int:
        steps, first, last = 0, cur, cur
        while first <= n:
            steps += min(last, n) - first + 1
            first *= 10
            last = last * 10 + 9
        return steps

    def findKthNumber(self, n: int, k: int) -> int:
        cur = 1
        k -= 1
        while k:
            steps = self.getSteps(cur, n)
            if steps <= k:
                k -= steps
                cur += 1
            else:
                cur *= 10
                k -= 1
        return cur
举报

相关推荐

0 条评论