[Python手撕]字典序的第K小数字
class Solution:
def findKthNumber(self, n: int, k: int) -> int:
def findk(n,k):
def countsteps(prefix,n):
start = prefix
end = prefix+1
steps = 0
while start <= n:
steps += min(n+1,end)-start
start *= 10
end *= 10
return steps
current = 1
k -= 1
while k > 0:
steps = countsteps(current, n)
if steps <= k:
current += 1
k -= steps
elif steps > k:
current *= 10
k -= 1
return current
return findk(n,k)

浙公网安备 33010602011771号