给定整数 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。
详见:https://leetcode.com/problems/k-th-smallest-in-lexicographical-order/description/

C++:

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;
}
};

参考:https://www.cnblogs.com/grandyang/p/6031787.html

最新文章

  1. JavaScript随笔8
  2. C++高精度计时器&mdash;&mdash;微秒级时间统计
  3. 报告一个IE很奇葩的滚动条问题——百分比计算宽度为浮点数时的滚动条显示异常
  4. 转自:C#中TextBox水印提示的简单实现
  5. Oberon程序设计语言简介
  6. boneCP的连接管理
  7. 201521123023《java程序设计》第三周学习总结
  8. Springboot 系列(十一)使用 Mybatis(自动生成插件) 访问数据库
  9. 快速查看linux命令的用法----------TLDR
  10. Percona XtraDB Cluster高可用与状态快照传输(PXC 5.7 )
  11. [剑指Offer]48-最长不含重复字符的子字符串(递归思想,循环实现)
  12. 破解验证码模拟登陆cnblogs
  13. 想3分钟搭建图像识别系统?这里有一份TensorFlow速成教程(转)
  14. Linux下idea选择tomcat server 报错Warning the selected directory is not a valid tomcat home
  15. 7-qt随机数qrand
  16. 在C#中判断某个类是否实现了某个接口
  17. JS 中document.URL 和 windows.location.href 的区别
  18. sqoop 数据迁移
  19. powershell网络钓鱼获取用户密码
  20. Java命令:Jstack

热门文章

  1. Visual Studio Visual assistant注释也做拼写检查怎么办
  2. Failed to load resource: the server responded with a status of 404 (Not Found)
  3. Android第一个个人APP(帐号助手)
  4. mount: wrong fs type
  5. Boost中的Timer的使用——计算时间流逝
  6. mysql10---索引优化
  7. UVALive3126 Taxi Cab Scheme —— 最小路径覆盖
  8. 一步一步学Silverlight 2系列(2):基本控件
  9. SPOJ:Fibonacci Polynomial(矩阵递推&amp;前缀和)
  10. python的thread和threading区别