博客
关于我
leetcode 440第k个字典序的数字【1】
阅读量:198 次
发布时间:2019-02-28

本文共 1589 字,大约阅读时间需要 5 分钟。

440第k个字典序的数字 

给定整数 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。

思路1:使用深度搜索,优势是可以输出序列,缺点是超时!!!

class Solution {	public:		int icount=0;		int res;		bool isstop=false;		int findKthNumber(int n, int k) {			dfs(0,n,k);			return res;		}		void dfs(int up,int n,int k) {			//if(isstop) return;			for(int i=0; i<10; i++) {				if(isstop) return;				int num=up*10+i;				if(num==0) continue;//打补丁				//cout<
<

思路2:其实这是一个"十叉树",如下图:

1)每个节点的子节点可以有:10个,比如节点1的子节点可以是10~19、节点2的字节的可以是20~29、。。。

但是由于n大小的限制,构成的并不是一个"满十叉树"。

2)分析题目中给的例子可以知道,数字1的子节点有4个(10,11,12,13),而后面的数字2到9都没有子节点,

那么这道题实际上就变成了一个先序遍历十叉树的问题。

3)那么,难点就变成了 计算出同一层两个相邻的节点的子节点的个数,也就是代码中的steps

  3.1)当前节点为 curr  (从curr = 1 开始),则同一层的下一个节点为 curr+1;

  3.2)计算节点 curr到节点curr+1之间的子节点个数steps

     3.2.1)如果子节点个数 大于 k,说明第k小的树一定在子节点中,

              继续向下一层寻找:curr *=10;

              k -= 1;(原因:向下一层寻找,肯定要减少前面的父节点,即 在上一层中的第k个数,在下一层中是第k-1个数)

     3.2.2)如果子节点个数 小于或者等于 k,说明第k小的树不在子节点中,

              继续向同一层下一个节点寻找:curr +=1;

              k -= steps;(原因:向下一层寻找,肯定要减少前面的所有的字节点)

   以此类推,直到k为0推出循环,此时cur即为所求。

class Solution {public:    int findKthNumber(int n, int k) {        int cur = 1;        --k;        while (k > 0) {            long long step = 0, first = cur, last = cur + 1;            while (first <= n) {                step += min((long long)n + 1, last) - first;                first *= 10;                last *= 10;            }            if (step <= k) {                ++cur;                k -= step;            } else {                cur *= 10;                --k;             }        }        return cur;    }};

 

你可能感兴趣的文章
Nginx/Apache反向代理
查看>>
Nginx: 413 – Request Entity Too Large Error and Solution
查看>>
nginx: [emerg] getpwnam(“www”) failed 错误处理方法
查看>>
nginx: [emerg] the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:
查看>>
nginx:Error ./configure: error: the HTTP rewrite module requires the PCRE library
查看>>
Nginx:objs/Makefile:432: recipe for target ‘objs/src/core/ngx_murmurhash.o‘解决方法
查看>>
Nginx、HAProxy、LVS
查看>>
nginx一些重要配置说明
查看>>
Nginx下配置codeigniter框架方法
查看>>
Nginx中使用expires指令实现配置浏览器缓存
查看>>
Nginx中实现流量控制(限制给定时间内HTTP请求的数量)示例
查看>>
nginx中配置root和alias的区别
查看>>
nginx主要流程(未完成)
查看>>
Nginx之二:nginx.conf简单配置(参数详解)
查看>>
Nginx从入门到精通
查看>>
Nginx代理websocket配置(解决websocket异常断开连接tcp连接不断问题)
查看>>
Nginx代理初探
查看>>
nginx代理地图服务--离线部署地图服务(地图数据篇.4)
查看>>
Nginx代理外网映射
查看>>
Nginx代理模式下 log-format 获取客户端真实IP
查看>>