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

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
上一篇:蓝队技能-应急响应篇&Web内存马查杀&Spring框架型&中间件型&JVM分析&Class提取


下一篇:WingetUI:可视化Windows常用的命令行包管理工具-04 项目地址