leet-code/src/main/java/leetcode/editor/cn/KThSmallestInLexicographicalOrder.java
2022-03-23 17:24:56 +08:00

76 lines
1.8 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//给定整数 n 和 k返回 [1, n] 中字典序第 k 小的数字。
//
//
//
// 示例 1:
//
//
//输入: n = 13, k = 2
//输出: 10
//解释: 字典序的排列是 [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9],所以第二小的数字是 10。
//
//
// 示例 2:
//
//
//输入: n = 1, k = 1
//输出: 1
//
//
//
//
// 提示:
//
//
// 1 <= k <= n <= 10⁹
//
// Related Topics 字典树 👍 389 👎 0
package leetcode.editor.cn;
//440:字典序的第K小数字
public class KThSmallestInLexicographicalOrder {
public static void main(String[] args) {
Solution solution = new KThSmallestInLexicographicalOrder().new Solution();
// TO TEST
System.out.println(solution.findKthNumber(13, 2));
System.out.println(solution.findKthNumber(1, 1));
System.out.println(solution.findKthNumber(100, 90));
System.out.println(solution.findKthNumber(10000, 10000));
}
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int findKthNumber(int n, int k) {
for (int i = 1; i <= n; i++) {
if (k == 1) {
return i;
}
int count = getCount(i, n);
if (count < k) {
k -= count;
} else {
i = i * 10 - 1;
k--;
}
}
return 0;
}
public int getCount(int num, long n) {
int count = 0;
long min = num;
long max = num;
while (min <= n) {
count += Math.min(max, n) - min + 1;
min = min * 10;
max = max * 10 + 9;
}
return count;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}