给定整数 n
和 k
,找到 1
到 n
中字典序第 k
小的数字。
注意:1 ≤ k ≤ n ≤ 109。
示例:
输入:
n: 13 k: 2
输出:
10
解释:
字典序的排列是 [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9],所以第二小的数字是 10。
解法1:
class Solution:
def findKthNumber(self, n: int, k: int) -> int:
# 字典序, 按 字典排序 1 10 100 ...2
# 1 <= k <= n <= 10^9;
# [1, n] 第k小的数字, 所以 字典序中数 不能超过n;
# prefix 和 n, [1, n]中第k个数(字典排序 也就是第k小的数)
# 十叉树
def cal_count(n, prefix, next_prefix):
count = 0
while prefix <= n:
count += min(next_prefix, n+1) - prefix # 计算个数 所以+1
prefix = prefix*10 # 下移一层
next_prefix = next_prefix * 10
return count
prefix = 1
k = k - 1 # 第k小, 索引-1
while k > 0:
step = cal_count(n, prefix, prefix+1) # 用来计算prefix移动到prefix+1需要移动多少步
if step <= k: # 同层移动
k = k - step
prefix = prefix + 1 # 本层 右移一个
else: # step > k; 往下层移动
k = k - 1 # 下移 要移动一次
prefix = prefix * 10
return prefix # prefix就是要找的值