image.png
    image.png

    1. class Solution {
    2. public int findKthNumber(int n, int k) {
    3. int ans = 1;
    4. while(k>1){
    5. int count = dfs(ans,n);
    6. //求出子树的数量和K比较,若大于k则在子树中找,否则在循环下一个节点
    7. if(count<k){
    8. ans++;
    9. k-=count;
    10. }else{
    11. ans*=10;
    12. k--;
    13. }
    14. }
    15. return ans;
    16. }
    17. //计算子树中的总数
    18. public int dfs(long ans,long n){
    19. int tem = 0;
    20. long left = n;
    21. long right = n;
    22. while(left<=right){
    23. tem += Math.min(right,n) - left +1;
    24. left*=10;
    25. right=right*10+9;
    26. }
    27. return tem;
    28. }
    29. }