Leetcode: 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. 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.
if the steps <= k, we know we can move to curr + 1, and narrow down k to k - steps.
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
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).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.
else if n2 > n, what means n (the biggest node) is on the path between n1 to n2, add (n + 1 - n1) to steps.
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的更多相关文章
- [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 ...
- [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 ...
- 【leetcode】1163. Last Substring in Lexicographical Order
题目如下: Given a string s, return the last substring of s in lexicographical order. Example 1: Input: & ...
- LeetCode 1061. Lexicographically Smallest Equivalent String
原题链接在这里:https://leetcode.com/problems/lexicographically-smallest-equivalent-string/ 题目: Given string ...
- 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- [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 ...
- [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 ...
随机推荐
- 深入浅出 - Android系统移植与平台开发(十) - led HAL简单设计案例分析
作者:唐老师,华清远见嵌入式学院讲师. 通过前两节HAL框架分析和JNI概述,我们对Android提供的Stub HAL有了比较详细的了解了,下面我们来看下led的实例,写驱动点亮led灯,就如同写程 ...
- Flex与.net进行URL参数传递编码处理
在JS中用到的三种编码方式escape 对应于Flex中是一样的,并且支持相互的解码 var a:String = "超越梦想#"; trace(escape(a)); //%u8 ...
- Centos 下面升级系统内核(转)
1.导入public key 1 rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org 2.安装ELRepo到CentOS 6. ...
- (转载)zeromq使用注意点滴
zeromq使用注意点滴 1.关于介绍zeromq的就不说了,可以自己去看官方guide很详细 2.主要说下在使用过程中需要注意的地方 1)使用如果使用c++的接口的时候,在你自己的类中或者apach ...
- Sumsets
题目连接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=91209#problem/A 题意:给定一个整数几何S,找出一个最大的d,使得a ...
- 浅谈iOS中的userAgent
浅谈iOS中的userAgent User-Agent(用户代理)字符串是Web浏览器用于声明自身型号版本并随HTTP请求发送给Web服务器的字符串,在Web服务器上可以获取到该字符串. 在公司产 ...
- 一次有趣的XSS漏洞挖掘分析(2)
第一次和一套程序做了这么多次的斗争.今天基友又给我来信说,没得玩了.了解了下情况,是他拿着0day到处插,被人家发现了.还出了个公告,说所有***必须安装补丁.呵呵,性福总是走的这么突然.这乐子一下就 ...
- ASIHTTPRequest类库简介和使用说明
官方网站: http://allseeing-i.com/ASIHTTPRequest/ .可以从上面下载到最新源码,以及获取到相关的资料. 使用iOS SDK中的HTTP网络请求API,相当的复杂, ...
- php——用for循环打印半金字塔、金字塔、正方形、倒金字塔、菱形、空心图形等
1.半金字塔 $n=5; //控制层数 for($i=1;$i<=$n;$i++){ //控制每层的 “*” 数 for($j=1;$j<=$i;$j++){ echo "*& ...
- html第一天
html为超文本标记语言, html下的内容不去分大小写: 标签必须要有开始和结束,<br>和<hr>特殊,在标签内结束<br/>,<hr/>:下划线 ...