Golang | Leetcode Golang题解之第440题字典序的第K小数字
题目:
题解:
func getSteps(cur, n int) (steps int) {
first, last := cur, cur
for first <= n {
steps += min(last, n) - first + 1
first *= 10
last = last*10 + 9
}
return
}
func findKthNumber(n, k int) int {
cur := 1
k--
for k > 0 {
steps := getSteps(cur, n)
if steps <= k {
k -= steps
cur++
} else {
cur *= 10
k--
}
}
return cur
}
func min(a, b int) int {
if a > b {
return b
}
return a
}