440 K-th Smallest in Lexicographical Order 字典序的第K小数字
2024-10-21 13:41:44
给定整数 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
最新文章
- JavaScript随笔8
- C++高精度计时器&mdash;&mdash;微秒级时间统计
- 报告一个IE很奇葩的滚动条问题——百分比计算宽度为浮点数时的滚动条显示异常
- 转自:C#中TextBox水印提示的简单实现
- Oberon程序设计语言简介
- boneCP的连接管理
- 201521123023《java程序设计》第三周学习总结
- Springboot 系列(十一)使用 Mybatis(自动生成插件) 访问数据库
- 快速查看linux命令的用法----------TLDR
- Percona XtraDB Cluster高可用与状态快照传输(PXC 5.7 )
- [剑指Offer]48-最长不含重复字符的子字符串(递归思想,循环实现)
- 破解验证码模拟登陆cnblogs
- 想3分钟搭建图像识别系统?这里有一份TensorFlow速成教程(转)
- Linux下idea选择tomcat server 报错Warning the selected directory is not a valid tomcat home
- 7-qt随机数qrand
- 在C#中判断某个类是否实现了某个接口
- JS 中document.URL 和 windows.location.href 的区别
- sqoop 数据迁移
- powershell网络钓鱼获取用户密码
- Java命令:Jstack
热门文章
- Visual Studio Visual assistant注释也做拼写检查怎么办
- Failed to load resource: the server responded with a status of 404 (Not Found)
- Android第一个个人APP(帐号助手)
- mount: wrong fs type
- Boost中的Timer的使用——计算时间流逝
- mysql10---索引优化
- UVALive3126 Taxi Cab Scheme —— 最小路径覆盖
- 一步一步学Silverlight 2系列(2):基本控件
- SPOJ:Fibonacci Polynomial(矩阵递推&;前缀和)
- python的thread和threading区别