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. Beginners Guide To Learn Dimension Reduction Techniques

    Beginners Guide To Learn Dimension Reduction Techniques Introduction Brevity is the soul of wit This ...

  2. c#实现Socket网络编程

    命名空间: 在网络环境下,我们最感兴趣的两个命名空间是System.Net和 System.Net.Sockets. System.Net命名空间通常与较高程的操作有关,例如download或uplo ...

  3. Shell 备忘录

    此文收集工作中用到的Shell备忘,随用随机: 1.比较 -eq       等于,如:if [ "$a" -eq "$b" ] -ne       不等于,如 ...

  4. Javascript里的那些距离们

    1.有滚动条的控件的距离: scrollTop和scrollLeft:分别指有滚动条的容器控件的滚动条的top和left:页面滚动条的通用取法:document.body.scrollTop(FF\C ...

  5. Delphi中@,^,#,$特殊符号意义

    概述:   ^: 指针   @: 取址   #: 十进制符   $: 十六进制符   @:取址运算符; var   int:integer;   p:^integer; begin   new(P); ...

  6. Linux Rsync

    一.Rsync介绍 1.什么是Rsync Rsync 即Remote Rynchronization,是一款开源的.快速的.多功能的.可实现全量或增量的本地或者远程数据镜像同步复制.备份的优秀工具. ...

  7. cf 403 D

    D. Beautiful Pairs of Numbers time limit per test 3 seconds memory limit per test 256 megabytes inpu ...

  8. POJ 3181 Dollar Dayz (完全背包,大数据运算)

    题意:给出两个数,n,m,问1~m中的数组成n,有多少种方法? 这题其实就相当于 UVA 674 Coin Change,求解一样 只不过数据很大,需要用到高精度运算... 后来还看了网上别人的解法, ...

  9. laravel where中多条件查询

    1. http://www.mobanstore.com/doc/bianchengkaifa/119.html //初学laravel 发现他的查询构造器很好用 //如下 $user = DB::t ...

  10. 深入浅出ES6(十六):模块 Modules

    作者 Jason Orendorff  github主页  https://github.com/jorendorff 早在2007年我刚加入Mozilla的JavaScript团队的时候广为流传一个 ...