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. JS获取节点方法

    1. 通过顶层document节点获取:(1) document.getElementById(elementId):该方法通过节点的ID,可以准确获得需要的元素,是比较简单快捷的方法.如果页面上含有 ...

  2. 对于Linux平台下C语言开发中__sync_函数的认识

      reference:http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/Atomic-Builtins.html#Atomic-Builtins A built ...

  3. linux 上传/下载文件到windows工具

    一般来说,linux服务器大多是通过ssh客户端来进行远程的登陆和管理的,使用ssh登陆linux主机以后,如何能够快速的和本地机器进行文件的交互呢,也就是上传和下载文件到服务器和本地:   与ssh ...

  4. 查看w3wp进程占用的内存及.NET内存泄露,死锁分析--转载

    一 基础知识 在分析之前,先上一张图: 从上面可以看到,这个w3wp进程占用了376M内存,启动了54个线程. 在使用windbg查看之前,看到的进程含有 *32 字样,意思是在64位机器上已32位方 ...

  5. visual studio 2012 Github

    前言 一直以来都想使用Git来管理自己平时积累的小代码,就是除了工作之外的代码了.有时候自己搞个小代码,在公司写了,就要通过U盘或者网盘等等一系列工具进行Copy,然后回家才能继续在原来的基础上作业. ...

  6. POJ 1503 Integer Inquiry(大数相加,java)

    题目 我要开始练习一些java的简单编程了^v^ import java.io.*; import java.util.*; import java.math.*; public class Main ...

  7. php laravel 安装

    windows环境尝试学习一下laravel 1.因为SAE的php版本为5.3,因此最高只能支持到Laravel4.1.x.(Laravel4.2用到了php5.4的trait特性) 以4.1为主. ...

  8. Struts2 Convention插件的使用(3)方法前的@Action注解

    package com.hyy.action; import org.apache.struts2.convention.annotation.Action; import com.opensymph ...

  9. BFS+贪心 HDOJ 5335 Walk Out

    题目传送门 /* 题意:求从(1, 1)走到(n, m)的二进制路径值最小 BFS+贪心:按照标程的作法,首先BFS搜索所有相邻0的位置,直到1出现.接下去从最靠近终点的1开始, 每一次走一步,不走回 ...

  10. java web线程池

    线程池 要知道在计算机中任何资源的创建,包括线程,都需要消耗系统资源的.在WEB服务中,对于web服 务器的响应速度必须要尽可能的快,这就容不得每次在用户提交请求按钮后,再创建线程提供服务 .为了减少 ...