440.字典序的第K小数字

题目

440.字典序的第K小数字

题目大意

给定整数 nk,返回 [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 < = 1 0 9 1 <= k <= n <= 10^9 1<=k<=n<=109

思路

利用字典树的特性将所有小于等于 n n n的数字按照字典序的方式进行重建,可以得到如下:(借用leetcode的官方题解的图)

image-20220323105513754

很显然,通过前序遍历就可以得到字典序从小到大的序列。遍历到第 i i i个节点就是第 i i i小的数字。对于节点 i i i,它的子节点的数字为 ( 10 × i , 10 × i + 1 , . . . , 10 × i + 9 ) (10\times i ,10\times i+1,...,10\times i+9) (10×i,10×i+1,...,10×i+9)。设当前的字典树的第 i i i小的节点值为 n o w now now,只需按照前序遍历再继续往后找 k − i k-i ki个节点即为目标节点。设与 n o w now now右侧第一个兄弟节点为 n o w + 1 now+1 now+1,前序遍历先遍历 n o w now now的子树,再遍历 n o w + 1 now+1 now+1的子树。设 n o w now now为根节点的子树的节点数为 c n t = C o u n t ( n o w , n ) cnt=Count(now,n) cnt=Count(now,n)

  • 如果 c n t < = k cnt<=k cnt<=k,则此时可以肯定第 k k k小的节点一定不在以 n o w now now为根的子树中,而在 n o w now now的右侧兄弟节点的子树中,则可以跳过 c n t cnt cnt个节点。从 n o w + 1 now+1 now+1继续查询。
  • 如果 c n t > k cnt>k cnt>k,则此时可以肯定第 k k k小的节点一定在以 n o w now now为根的子树中。从 n o w now now儿子节点的左侧第一个孩子开始往后查找 k − i − 1 k - i - 1 ki1个节点,此时左侧第一个孩子的值为 10 × n o w 10 \times now 10×now
  • 依次重复上述操作直到找到 k k k小的节点即可。

ll Count(ll cur,ll n)函数:按照层次遍历子树, s s s指向第$ i $层的最左侧的孩子节点, t t t指向第 i i i 层的最右侧的孩子节点,那么 s = s ∗ 10 , t = t ∗ 10 + 9 s=s*10,t=t*10+9 s=s10,t=t10+9,第 i i i层共 t − s + 1 t-s+1 ts+1个节点,但是注意 t = m i n ( t , n ) t=min(t,n) t=min(t,n)。不断循环直到 s > n s>n s>n终止循环。

代码

// short int long float double bool char string void
// array vector stack queue auto const operator
// class public private static friend extern 
// sizeof new delete return cout cin memset malloc
// relloc size length memset malloc relloc size length
// for while if else switch case continue break system
// endl reverse sort swap substr begin end iterator
// namespace include define NULL nullptr exit equals 
// index col row arr err left right ans res vec que sta
// state flag ch str max min default charray std
// maxn minn INT_MAX INT_MIN push_back insert
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int>PII;
typedef pair<int, string>PIS;
const int maxn=1e5+50;//注意修改大小
long long read(){long long x=0,f=1;char c=getchar();while(!isdigit(c)){if(c=='-') f=-1;c=getchar();}while(isdigit(c)){x=x*10+c-'0';c=getchar();}return x*f;}
ll qpow(ll x,ll q,ll Mod){ll ans=1;while(q){if(q&1)ans=ans*x%Mod;q>>=1;x=(x*x)%Mod;}return ans%Mod;}

class Solution {
public:
    ll Count(ll cur,ll n){
        ll cnt=0,s=cur,t=cur;
        while(s<=n){
            cnt+=min(t,n)-s+1;
            s*=10;
            t=t*10+9;
        }
        return cnt;
    }
    int findKthNumber(int n, int k) {
        int now=1;
        k--;
        while(k){
            int cnt=Count(now,n);
            if(cnt<=k){
                k-=cnt;
                now++;
            }
            else{
                now*=10;
                k--;
            }
        }
        return now;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Phoenix_ZengHao

创作不易,能否打赏一瓶饮料?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值