Given integers n and k, find the lexicographically k-th smallest integer in the range from 1 to n.

Note: 1 ≤ k ≤ n ≤ 109.

Example:

Input:
n: 13 k: 2 Output:
10 Explanation:
The lexicographical order is [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9], so the second smallest number is 10.

第二遍做法:参考https://discuss.leetcode.com/topic/64624/concise-easy-to-understand-java-5ms-solution-with-explaination

Actually this is a denary tree (each node has 10 children). Find the kth element is to do a k steps preorder traverse of the tree.

Initially, image you are at node 1 (variable: curr),
the goal is move (k - 1) steps to the target node x. (substract steps from k after moving)
when k is down to 0, curr will be finally at node x, there you get the result.

we don't really need to do a exact k steps preorder traverse of the denary tree, the idea is to calculate the steps between curr and curr + 1 (neighbor nodes in same level), in order to skip some unnecessary moves.

Main function
Firstly, calculate how many steps curr need to move to curr + 1.

  1. if the steps <= k, we know we can move to curr + 1, and narrow down k to k - steps.

  2. else if the steps > k, that means the curr + 1 is actually behind the target node x in the preorder path, we can't jump to curr + 1. What we have to do is to move forward only 1 step (curr * 10 is always next preorder node) and repeat the iteration.

calSteps function

    1. how to calculate the steps between curr and curr + 1?
      Here we come up a idea to calculate by level.
      Let n1 = curr, n2 = curr + 1.
      n2 is always the next right node beside n1's right most node (who shares the same ancestor "curr")
      (refer to the pic, 2 is right next to 1, 20 is right next to 19, 200 is right next to 199).

    2. so, if n2 <= n, what means n1's right most node exists, we can simply add the number of nodes from n1 to n2 to steps.

    3. else if n2 > n, what means n (the biggest node) is on the path between n1 to n2, add (n + 1 - n1) to steps.

    4. organize this flow to "steps += Math.min(n + 1, n2) - n1; n1 *= 10; n2 *= 10;"

 public class Solution {
public int findKthNumber(int n, int k) {
int curr = 1;
k--;
while (k > 0) {
int steps = calc(n, curr, curr+1);
if (k >= steps) {
k -= steps;
curr = curr + 1;
}
else {
k -= 1;
curr = curr * 10;
}
}
return curr;
} public int calc(int n, long n1, long n2) {
int steps = 0;
while (n1 <= n) {
steps += Math.min(n+1, n2) - n1;
n1 *= 10;
n2 *= 10;
}
return steps;
}
}

下面是我自己的方法,参考Lexicographical Numbers这道题,对是对的,但是挨个访问,没有skip, TLE了

 public class Solution {
public int findKthNumber(int n, int k) {
int cur = 1;
for (int i=1; i<k; i++) {
if (cur * 10 <= n) {
cur = cur * 10;
}
else {
while (cur>10 && cur%10==9) {
cur /= 10;
}
cur = cur + 1;
}
}
return cur;
}
}

Leetcode: 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. [LeetCode] 786. K-th Smallest Prime Fraction 第K小的质分数

    A sorted list A contains 1, plus some number of primes.  Then, for every p < q in the list, we co ...

  5. 【leetcode】1163. Last Substring in Lexicographical Order

    题目如下: Given a string s, return the last substring of s in lexicographical order. Example 1: Input: & ...

  6. LeetCode 1061. Lexicographically Smallest Equivalent String

    原题链接在这里:https://leetcode.com/problems/lexicographically-smallest-equivalent-string/ 题目: Given string ...

  7. 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

  8. [LeetCode] 378. Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...

  9. [LeetCode] Find K-th Smallest Pair Distance 找第K小的数对儿距离

    Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pai ...

随机推荐

  1. BZOJ4517: [Sdoi2016]排列计数

    Description 求有多少种长度为 n 的序列 A,满足以下条件: 1 ~ n 这 n 个数在序列中各出现了一次 若第 i 个数 A[i] 的值为 i,则称 i 是稳定的.序列恰好有 m 个数是 ...

  2. SpringMVC异常处理机制详解[附带源码分析]

    目录 前言 重要接口和类介绍 HandlerExceptionResolver接口 AbstractHandlerExceptionResolver抽象类 AbstractHandlerMethodE ...

  3. 简单实现异步编程promise模式

    本篇文章主要介绍了异步编程promise模式的简单实现,并对每一步进行了分析,需要的朋友可以参考下 异步编程 javascript异步编程, web2.0时代比较热门的编程方式,我们平时码的时候也或多 ...

  4. html5的本地存储

    转载1:http://www.cnblogs.com/fly_dragon/p/3946012.html 转载2:http://www.cnblogs.com/xiaowei0705/archive/ ...

  5. 好吧,CSS3 3D transform变换,不过如此!

    一.写在前面的秋裤 早在去年的去年,我就大肆介绍了2D transform相关内容.看过海贼王的都知道,带D的家伙都不是好惹的,2D我辈尚可以应付,3D的话,呵呵,估计我等早就在千里之外被其霸气震晕了 ...

  6. IIS 8 下使用 WCF

    按照以下步骤添加后,IIS8即支持WCF服务. 首先添加MIME类型 扩展名“.svc”,MIME类型 “application/octet-stream” 2. 然后在“Handler Mappin ...

  7. 学习Jquery

    早就听说了Jquery的大名,一直没有细心的学习一下,通过阅读收集的一些资料,感觉Jquery真的很强大.决定开始自己的学习Jquery之旅.在这里不是为大家讲解Jquery(深知水平有限),只是将自 ...

  8. c++ basic 整理1

    //c++类 初始化 与 初始赋值 //C++规定 对象的 成员变量 初始化 动作发生 在进入 构造函数本体 之前 . 在构造函数内 成员变量赋值都属于 赋值 . class People { pub ...

  9. Centos7 创建个文件 thread 怪现象

    我在~下创建个目录thread, 然后写了个程序, 再执行, 提示需要权限, 之后我重命名下文件的名字, 重新编译下, 就可以正常执行了.

  10. 本地化word复制来的网页中的图片

    复制一个网页到word文档中,图片会以链接到网页里图片,而不是本地化保存在文档里.为了让图片存在文档里,而不是每次链接到外部,可以这样做. 全选文档,菜单栏里的编辑,点击链接,断开所有链接. 然后再复 ...