前言
面试总结第六波,LeetCode相关的面试常考题总结。
面试常考题
目录
LeetCode 470 用Rand7()实现Rand10()
LeetCode 440 字典序的第k小数字
题目:https://leetcode-cn.com/problems/k-th-smallest-in-lexicographical-order/
给定整数 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。
class Solution {
public:
int findKthNumber(int n, int k) {
int cur=1;
k--;
while(k){
int steps=get_steps(n,cur,cur+1); //计算当前节点下的子节点个数
if(steps<=k){ //若小于等于k,则在下一