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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 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 ...

  4. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  5. Hard模式题目

    先过一下Hard模式的题目吧.   # Title Editorial Acceptance Difficulty Frequency   . 65 Valid Number     12.6% Ha ...

  6. 继续过Hard题目.周五

      # Title Editorial Acceptance Difficulty Frequency   . 65 Valid Number     12.6% Hard    . 126 Word ...

  7. leetcode 学习心得 (2) (301~516)

    源代码地址:https://github.com/hopebo/hopelee 语言:C++ 301. Remove Invalid Parentheses Remove the minimum nu ...

  8. 【LeetCode】栈 stack(共40题)

    [20]Valid Parentheses (2018年11月28日,复习, ko) 给了一个字符串判断是不是合法的括号配对. 题解:直接stack class Solution { public: ...

  9. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

随机推荐

  1. MySQL 5.7 reference about JSON

    最近需要用到MySQL 5.7中的JSON,总结一下MySQL中关于JSON的内容 参考: 11.6 The JSON Data Type 12.16 JSON Functions JSON Func ...

  2. asp.net MVC3 + JQuery 的ajax简单使用

    一直都没有使用过JQuery,更没使用过JQuery的ajax支持带来的方便,今天试了一下,真是减少了很多工作量,使用方法也比较简单 这里先记下来,以后使用时可以再拿着用. 本应用中,本来是准备使用长 ...

  3. Unity3D 错误,nativeVideoFrameCallback解决方法。

    原地址:http://blog.csdn.net/alking_sun/article/details/23684733 Unity3D在打包安卓应用的时候,一打开游戏就闪退,接入LogCat之后发现 ...

  4. Sublime Text3激活 破解

    Sublime Text 是一个复杂的文本.代码编辑器.出色用户界面,非凡的功能和惊人的性能. Sublime Text 3 官方网站 http://www.sublimetext.com/ 点击菜单 ...

  5. 将HTMLCollection/NodeList/伪数组转换成数组

    这里把符合以下条件的对象称为伪数组(ArrayLike) 1,具有length属性 2,按索引方式存储数据 3,不具有数组的push,pop等方法 如 1,function内的arguments . ...

  6. 机器学习在 IT 运维管理中的必要性!

    机器学习技术在监控工具中的应用已经成为 IT 运维与 DevOps 团队的一大热点话题.尽管相关的使用案例很多,对 IT 团队而已真正的「杀手级应用」是机器学习如何提高实时事件管理能力,从而帮助较大规 ...

  7. c# 获取mac地址的2种方法

    和大家分享下,互相学习一下吧.第一个获取方法好像获取不到mac地址,我用了第二种方法可以获取到.希望知道的可以说下为什么. 1,首先要添加引用:using System.Management; 2,代 ...

  8. Xcode显示行号

  9. POJ 1300 Door Man(欧拉回路的判定)

    题目链接 题意 : 庄园有很多房间,编号从0到n-1,能否找到一条路径经过所有开着的门,并且使得通过门之后就把门关上,关上的再也不打开,最后能回到编号为0的房间. 思路 : 这就是一个赤裸裸的判断欧拉 ...

  10. Task 使用 Task以及Task.Factory都是在.Net 4引用的。Task跟Thread很类似,通过下面例子可以看到。

    static public void ThreadMain() { Thread t1 = new Thread(TaskWorker); t1.Start(3); } static public v ...