当前位置:网站首页>440. The k-th small number of dictionary order (difficult) - dictionary tree - number node - byte skipping high-frequency question
440. The k-th small number of dictionary order (difficult) - dictionary tree - number node - byte skipping high-frequency question
2022-04-23 17:33:00 【hequnwang10】
One 、 Title Description
Given integer n and k, return [1, n] Chinese dictionary order No k Small numbers .
Example 1:
Input : n = 13, k = 2
Output : 10
explain : The order of the dictionary is [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9], So the second smallest number is 10.
Example 2:
Input : n = 1, k = 1
Output : 1
Example 3:
Input :target = 11, nums = [1,1,1,1,1,1,1,1]
Output :0
Two 、 Problem solving
Dictionary tree
First of all, let's look at 386 topic . This question is No k Small numbers , In fact, it is to find the number of child nodes of each node in the tree , Then with K Value is compared with .

Among them, the first node of the first layer 1 Calculation , Look at it 1 Is the number of child nodes below the node . If the number of child nodes of the current node is greater than K, Then the target value is under the subtree , Then look down layer by layer , Update node values ,1->10, It also needs to be updated K value , That is, the first number of the second time 0, Then calculate the 0 How many child node values are there below , Keep on executing , Until the final value is found .
If the first node of the first layer 1 The number of child nodes below the value is less than K value , Description at node 2 Next look , to update K value , Execute sequentially .
If it's under the first node of the first layer , Just look down on each floor in turn ;
class Solution {
public int findKthNumber(int n, int k) {
long cur = 1;
k--;
while(k > 0){
int nodes = countNodes(n,cur);
if(k >= nodes){
k = k - nodes;
cur++;
}else{
k--;
cur *= 10;
}
}
return (int) cur;
}
public int countNodes(long n,long cur){
long total = 0;
long next = cur + 1;
while(cur <= n){
total += Math.min(n - cur + 1,next -cur);
cur *= 10;
next *= 10;
}
return (int) total;
}
}

Output test :
Code :
class Solution {
public int findKthNumber(int n, int k) {
long cur = 1;
k--;
System.out.println("0->k:"+k);
while(k > 0){
int nodes = countNodes(n,cur);
System.out.println("nodes:"+nodes);
if(k >= nodes){
k = k - nodes;
System.out.println("1->k:"+k);
cur++;
System.out.println("1->cur:"+cur);
}else{
k--;
System.out.println("2->k:"+k);
cur *= 10;
System.out.println("2->cur:"+cur);
}
}
return (int) cur;
}
public int countNodes(long n,long cur){
long total = 0;
long next = cur + 1;
while(cur <= n){
// Calculate the number of nodes in each layer , Then count the initial cur Value less than n Of nodes
total += Math.min(n - cur + 1,next -cur);
System.out.println("total:"+total);
cur *= 10;
next *= 10;
}
return (int) total;
}
}
0->k:65
total:1
total:11
total:35
nodes:35
1->k:30
1->cur:2
total:1
total:11
nodes:11
1->k:19
1->cur:3
total:1
total:11
nodes:11
1->k:8
1->cur:4
total:1
total:11
nodes:11
2->k:7
2->cur:40
total:1
nodes:1
1->k:6
1->cur:41
total:1
nodes:1
1->k:5
1->cur:42
total:1
nodes:1
1->k:4
1->cur:43
total:1
nodes:1
1->k:3
1->cur:44
total:1
nodes:1
1->k:2
1->cur:45
total:1
nodes:1
1->k:1
1->cur:46
total:1
nodes:1
1->k:0
1->cur:47
analysis : The first node of the first layer 1 The number of child nodes is 35, Less than K value , Then continue on node 2 Search for , to update k The value is 30, And then the nodes 2 The number of child nodes is 11, Less than 30, Then continue on node 3 Search for , And then update K The value is 19, In turn
版权声明
本文为[hequnwang10]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231732009099.html
边栏推荐
- 386. 字典序排数(中等)-迭代-全排列
- JS, entries(), keys(), values(), some(), object Assign() traversal array usage
- Oninput one function to control multiple oninputs (take the contents of this input box as parameters) [very practical, very practical]
- Use of shell cut command
- Shell-入门、变量、以及基本的语法
- Further optimize Baidu map data visualization
- 嵌入式系统中,FLASH中的程序代码必须搬到RAM中运行吗?
- 122. 买卖股票的最佳时机 II-一次遍历
- Clickhouse - data type
- 470. 用 Rand7() 实现 Rand10()
猜你喜欢
![Using quartz under. Net core -- job attributes and exceptions of [4] jobs and triggers](/img/ec/43dddd18f0ce215f0f1a781e31f6a8.png)
Using quartz under. Net core -- job attributes and exceptions of [4] jobs and triggers

102. 二叉树的层序遍历

SiteServer CMS5. 0 Usage Summary

Summary of common SQL statements

双闭环直流调速系统matlab/simulink仿真

Simulation of infrared wireless communication based on 51 single chip microcomputer

Use of five routing guards

EF core in ASP Generate core priority database based on net entity model

958. 二叉树的完全性检验

Webapi + form form upload file
随机推荐
MySQL installation
01-初识sketch-sketch优势
El date picker limits the selection range from the current time to two months ago
Future 用法详解
Exercise: even sum, threshold segmentation and difference (two basic questions of list object)
41. The first missing positive number
01 - get to know the advantages of sketch sketch
Shell - introduction, variables, and basic syntax
C# Task. Delay and thread The difference between sleep
Baidu Map Case - Zoom component, map scale component
Why do some people say SCM is simple and I have to learn it so hard?
ECMAScript history
Using quartz under. Net core - calendar of [6] jobs and triggers
Open futures, open an account, cloud security or trust the software of futures companies?
给 el-dialog 增加拖拽功能
[C] thoroughly understand the deep copy
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
[logical fallacy in life] Scarecrow fallacy and inability to refute are not proof
Perception of linear algebra 2
386. 字典序排数(中等)-迭代-全排列