当前位置:网站首页>2022-02-05: the k-th decimal number of dictionary order. Given integers n and K, find 1
2022-02-05: the k-th decimal number of dictionary order. Given integers n and K, find 1
2022-06-23 02:30:00 【Fuda scaffold constructor's daily question】
2022-02-05: The second part of the dictionary order K Small number .
Given integer n and k, find 1 To n Chinese dictionary order No k Small numbers .
Be careful :1 ≤ k ≤ n ≤ 10**9.
Example :
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.
Power button 440.
answer 2022-02-05:
This problem is hard to think of . See code for details .
Divide into left , in , The third part on the right .
Time complexity :O(logN). The question is leetcode On , All the solutions can only be done O( (logN) square ) Solution .
The code to use golang To write . The code is as follows :
package main
import "fmt"
func main() {
n := 13
k := 2
ret := findKthNumber(n, k)
fmt.Println(ret)
}
var offset = []int{0, 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000}
var number = []int{0, 1, 11, 111, 1111, 11111, 111111, 1111111, 11111111, 111111111, 1111111111}
func findKthNumber(n, k int) int {
// Numbers num, How many are there ,len position
// 65237, 5 position ,len = 5
len0 := lenf(n)
// 65237, Beginning number ,6,first
first := n / offset[len0]
// 65237, There are a few on the left ?
left := (first - 1) * number[len0]
pick := 0
already := 0
if k <= left {
// k / a Rounding up -> (k + a - 1) / a
pick = (k + number[len0] - 1) / number[len0]
already = (pick - 1) * number[len0]
return kth((pick+1)*offset[len0]-1, len0, k-already)
}
mid := number[len0-1] + (n % offset[len0]) + 1
if k-left <= mid {
return kth(n, len0, k-left)
}
k -= left + mid
len0--
pick = (k+number[len0]-1)/number[len0] + first
already = (pick - first - 1) * number[len0]
return kth((pick+1)*offset[len0]-1, len0, k-already)
}
func lenf(n int) int {
len0 := 0
for n != 0 {
n /= 10
len0++
}
return len0
}
func kth(max int, len0 int, kth0 int) int {
// The middle range doesn't matter !
// Any step , Missed in the middle , Left or right hit , After that, I can't control it !
// But at first , It must be in charge !
closeToMax := true
ans := max / offset[len0]
for dinc(kth0) > 0 {
kth0--
max %= offset[len0]
len0--
pick := 0
if !closeToMax {
pick = (kth0 - 1) / number[len0]
ans = ans*10 + pick
kth0 -= pick * number[len0]
} else {
first := max / offset[len0]
left := first * number[len0]
if kth0 <= left {
closeToMax = false
pick = (kth0 - 1) / number[len0]
ans = ans*10 + pick
kth0 -= pick * number[len0]
continue
}
kth0 -= left
mid := number[len0-1] + (max % offset[len0]) + 1
if kth0 <= mid {
ans = ans*10 + first
continue
}
closeToMax = false
kth0 -= mid
len0--
pick = (kth0+number[len0]-1)/number[len0] + first
ans = ans*10 + pick
kth0 -= (pick - first - 1) * number[len0]
}
}
return ans
}The results are as follows :
边栏推荐
- II Data preprocessing
- Goframe framework (RK boot): fast implementation of CSRF verification
- 5g core network and core network evolution
- How to prohibit copying and copying files to the local server remote desktop
- Digital integrated circuit design process
- Performance test -- 14 detailed explanation of performance test report and precautions
- Reptile lesson 1
- C language series - Section 4 - arrays
- Buuctf misc-[actf freshman competition 2020]outline
- Microservice Optimization: internal communication of microservices using grpc
猜你喜欢

C language games: sanziqi (simple version) implementation explanation

Information theory and coding

Xgboost Guide

Performance test -- Jenkins environment construction for 15jmeter performance test

Application and challenge of ten billion level map data in Kwai security intelligence

Understand GB, gbdt and xgboost step by step

CSDN browser assistant for online translation, calculation, learning and removal of all advertisements

Performance testing -- Interpretation and practice of 16 enterprise level project framework

Interviewer: with the for loop, why do you need foreach??

My good brother gave me a difficult problem: retry mechanism
随机推荐
1. Mx6u startup mode and equipment
Anaconda creates a new environment encounter pit
Buuctf misc-[actf freshman competition 2020]outline
SetTimeout and setinterval execution time
How to set up an H5 demo of easyplayer locally to play h265 video streams?
Exploit format string vulnerability in CDE
5g spectrum
There is no WM data in the primary commodity master data store view of SAP retail
An article shows you the difference between high fidelity and low fidelity prototypes
Analysis of resolv Conf common parameters
How to batch generate matrix 25 codes
8 vertical centering methods
Stop automatically after MySQL starts (unable to start)
Hypervisor Necromancy; Recover kernel protector (1)
Rebirth -- millimeter wave radar and some things I have to say
51. numerical arrangement
How to make traditional Chinese medicine labels with pictures
Analysis of web page status code
Information theory and coding
Performance test -- Jenkins environment construction for 15jmeter performance test