没想到这道题竟然这么难。
对于我这道题的难点就是分清字典序的标号和本身节点所代表的数值大小
两个红色下标所代指的就是两个节点前缀之差即为一颗子树在字典序中有多少个点,这一点明白以后,大致在外部函数计算子树大小时就没有这么多疑惑了。
下面最重要的就是分清下标和数值大小。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll ;
class Solution {
public:
ll cal(ll x , ll n){
ll cur = x , nxt = x + 1,count = 0 ;
while(cur <= n){
count += min(n + 1, nxt) - cur;
cur *= 10;
nxt *= 10;
}
return count;
}
int findKthNumber(int n, int k) {
int ans = 1 , now = 1; ///
while(now < k){
ll q = cal(ans, n);
// cout<< "ans = " << ans << " now == " << now << " q = " << q << endl;
if(q + now > k){
ans *= 10;
now ++ ;
}
else{
ans ++ ;
now += q;
}
}
return ans;
}
};
//int main(){
// int n , k ;
// while(~scanf("%d %d" , &n , &k)){
// printf("%d\n",findKthNumber(n,k));
// }
// return 0;
//
//}