非常非常好!写了好久 k-th-smallest-in-lexicographical-order
https://leetcode.com/problems/k-th-smallest-in-lexicographical-order/
我没做出来。还是脑子不够清醒。
下面这个解法真的很棒很棒。
https://discuss.leetcode.com/topic/64442/easy-to-understand-js-solution
原理可以参照下面这个看,就更清楚了
https://discuss.leetcode.com/topic/64462/c-python-0ms-o-log-n-2-time-o-1-space-super-easy-solution-with-detailed-explanations
我开始只是想第一个数字找到个数。实际上第一个找到之后,可以仍然保留作为前缀,每次看相同前缀的数字存在多少个。
这个过程,代码写的好艰辛。
虽然从上面看了思路,但是真正写的时候,还是发现有大大小小的坑。很多细节和corner case需要处理。
开始听着音乐写,头越来越晕,还是不行。
1. 使用了一个count来获得前缀是 xyz,并且 <=n 的情况下,有多少次出现。
注意使用 (prefix+1) * step <= n+1 来检查,并且在循环结束后检查一下,是否需要加上最后余下的一些个数(具体见下面代码的实现)。
这个结果会用来每次对于k进行减除。而如果count是小于等于k的情况,就保留这个前缀,因为这个前缀一定会出现在最后结果中。然后再下一轮循环。
2. 对于0的处理,我的代码里面用了 prefix==0直接返回count为0来处理。并且检查是否正好k==1返回前缀prefix作为结果时,只针对prefix>0.
3. 对于 xx 和 xxi 的区分(其中i是0-9的数字)。仔细分析,发现总是有一个xx在最前面,不需要加i。并且占了一位。
所以对于k最后是1的情况,直接返回xx就可以了;如果k>1,那就把xx所占的一位减掉,然后再加后缀来检查。
最后,count函数对于 xx * 10 + i 还溢出了,需要用long类型才可以。
代码如下:
package com.company; //https://discuss.leetcode.com/topic/64442/easy-to-understand-js-solution
//https://discuss.leetcode.com/topic/64462/c-python-0ms-o-log-n-2-time-o-1-space-super-easy-solution-with-detailed-explanations class Solution { // 开始prefix写成 int,超出了
private int getCount(int n, long prefix) {
if (prefix == 0) {
return 0;
} int count = 0;
int step = 1;
while ((prefix+1) * step <= n+1) {
count += step;
step *= 10;
}
if (prefix * step <= n) {
count += n + 1 - prefix * step;
}
return count;
} public int findKthNumber(int n, int k) { int prefix = 0; while (k > 0) {
if (prefix != 0) {
if (k == 1) {
return prefix;
}
else {
k--;
}
} for (int i=0; i<10; i++) { int count = getCount(n, prefix*10+i);
if (k > count) {
k -= count;
}
else {
prefix = prefix * 10 + i;
break;
}
}
}
return prefix;
}
} public class Main { public static void main(String[] args) {
// write your code here
System.out.println("Hello");
Solution solution = new Solution(); int n = 681692778, k = 351251360;
int ret = solution.findKthNumber(n, k);
System.out.printf("Result is %d\n", ret); }
}
非常非常好!写了好久 k-th-smallest-in-lexicographical-order的更多相关文章
- [LeetCode] K-th Smallest in Lexicographical Order 字典顺序的第K小数字
Given integers n and k, find the lexicographically k-th smallest integer in the range from 1 to n. N ...
- [Swift]LeetCode440. 字典序的第K小数字 | K-th Smallest in Lexicographical Order
Given integers n and k, find the lexicographically k-th smallest integer in the range from 1 to n. N ...
- 440 K-th Smallest in Lexicographical Order 字典序的第K小数字
给定整数 n 和 k,找到 1 到 n 中字典序第 k 小的数字.注意:1 ≤ k ≤ n ≤ 109.示例 :输入:n: 13 k: 2输出:10解释:字典序的排列是 [1, 10, 11, 1 ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- Hard模式题目
先过一下Hard模式的题目吧. # Title Editorial Acceptance Difficulty Frequency . 65 Valid Number 12.6% Ha ...
- 继续过Hard题目.周五
# Title Editorial Acceptance Difficulty Frequency . 65 Valid Number 12.6% Hard . 126 Word ...
- leetcode 学习心得 (2) (301~516)
源代码地址:https://github.com/hopebo/hopelee 语言:C++ 301. Remove Invalid Parentheses Remove the minimum nu ...
- 【LeetCode】栈 stack(共40题)
[20]Valid Parentheses (2018年11月28日,复习, ko) 给了一个字符串判断是不是合法的括号配对. 题解:直接stack class Solution { public: ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
随机推荐
- C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V110\Microsoft.CppCommon.targets(611,5): error MSB
project options, linker, manifest, Generate Manifest-> NO. 项目->属性->链接器->清单文件->生成清单 改 ...
- The Brain vs Deep Learning Part I: Computational Complexity — Or Why the Singularity Is Nowhere Near
The Brain vs Deep Learning Part I: Computational Complexity — Or Why the Singularity Is Nowhere Near ...
- Struct2、Hibernate3、Spring3框架搭建实战(转)
采用目前最新的struts-2.3.1.2.hibernate3.6.10.Final.spring-framework-3.1.1.RELEASE开发包,以及eclipse-jee-indigo-S ...
- webvector将html转为svg或者png图片的工具
有些js较多,html组织不好的页面转换起来很不理想,cnblog转换的还不错 http://cssbox.sourceforge.net/webvector/
- 比较IE6的不同之处,与IE8 IE11 比较
文档申明为 <!DOCTYPE html> IE6或者IE特有的一些东西 1.盒子模型 IE6:(使用 !DOCTYPE 声明指定 standards-compliant 模式) mar ...
- 如何使用CSS3创建一个漂亮的图标
演示 下载 今天,我想展示给你一个巧妙的花招.我们将创建一个纯CSS3文本图标.更让人震惊的是,这效果将只需要一个HTML元素. 游戏的计划 创建一个矩形盒子 设置圆角 使用伪类元素创建一个卷角效果 ...
- 去“IOE”
所谓去“IOE”,是对去“IBM.Oracle.EMC”的简称,三者均为海外IT巨头,其中IBM代表硬件以及整体解决方案服务商,Oracle代表数据库,EMC代表数据存储.去“IOE”策略更广泛的理解 ...
- idHTTP访问百度
百度屏蔽了indy的客户端标识的 Mozilla/3.0 (compatible; Indy Library),把‘Indy Library’去掉就可以了. try IdHTTP1.Request.U ...
- Windows 7 常用快捷键 命令
Win+E: 打开新的windows资源浏览器 Win+F:搜索文件或文件夹 Win+R:打开运行窗口 Win + D:显示桌面 Win + M:最小化所有窗口 Ctrl+Shift+N: 新建文件 ...
- 我是如何学习 Linux 的
为何要学习 Linux? 这个问题可能困扰着很多 Linux 初学者和爱好者,其实我也说不上来为何要学习 Linux,可能最实在的理由就是—-Linux 相关工作岗位很多.在“见到” Linux 的第 ...