当前位置:网站首页>2021-08-27: the normal odometer will display natural numbers in turn to indicate mileage, Kyrgyzstan
2021-08-27: the normal odometer will display natural numbers in turn to indicate mileage, Kyrgyzstan
2022-06-24 04:59:00 【Fuda scaffold constructor's daily question】
2021-08-27: The normal odometer will display natural numbers in turn, indicating mileage , An auspicious odometer will ignore 4 And jump to the next one without 4 Number of numbers . normal :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 X. auspicious :1 2 3 5 6 7 8 9 10 11 12 13 15 16 17 ... 38 39 50 51 52 53 55. Give a lucky odometer number num( Of course, this number does not contain 4). Return the actual mileage represented by this number .
Fuda answer 2021-08-27:
The essence of this problem is a 9 The base number is converted to 10 Binary number .
0-3 unchanged .5-9 become 4-8.
such as 39, First become 38, then 3*9+8=35.35 Is the value you need to return .
Time complexity :O(lgN).
Spatial complexity :O(1).
The code to use golang To write . The code is as follows :
package main
import "fmt"
func main() {
ret := notContains4Nums1(39)
fmt.Println(ret)
ret = notContains4Nums2(39)
fmt.Println(ret)
ret = notContains4Nums3(39)
fmt.Println(ret)
}
// num There must be no 4 This number
func notContains4Nums1(num int) int {
count := 0
for i := 1; i <= num; i++ {
if isNot4(i) {
count++
}
}
return count
}
func isNot4(num int) bool {
for num != 0 {
if num%10 == 4 {
return false
}
num /= 10
}
return true
}
// arr[1] : Than 1 The number of digits is still small 1 position , There are several numbers ( Numbers can contain 0 But it cannot contain 4)?0 individual
// arr[2] : Than 2 The number of digits is still small 1 position , There are several numbers ( Numbers can contain 0 But it cannot contain 4)?9 individual
// 1 -> 0 1 2 3 - 5 6 7 8 9 = 9
// arr[3] : Than 3 The number of digits is still small 1 position , There are several numbers ( Numbers can contain 0 But it cannot contain 4)?81 individual
// 1 : 0 (0 1 2 3 - 5 6 7 8 9) = 9
// 2 :
// 1 (0 1 2 3 - 5 6 7 8 9) = 9
// 2 (0 1 2 3 - 5 6 7 8 9) = 9
// 3 (0 1 2 3 - 5 6 7 8 9) = 9
// 5 (0 1 2 3 - 5 6 7 8 9) = 9
// ...
// 9 (0 1 2 3 - 5 6 7 8 9) = 9
var arr []int = []int{0, 1, 9, 81, 729, 6561, 59049, 531441, 4782969, 43046721, 387420489,
3486784401, 31381059609, 282429536481, 2541865828329, 22876792454961, 205891132094649,
1853020188851841, 16677181699666569, 150094635296999121, 1350851717672992089}
// num There must be no 4 This number
func notContains4Nums2(num int) int {
if num <= 0 {
return 0
}
// num The decimal digits of ,len
len2 := decimalLength(num)
// 35621
// 10000
offset := offset(len2)
// First digit
first := num / offset
return arr[len2] - 1 + (first-twoSelectOne(first < 4, 1, 2))*arr[len2] + process(num%offset, offset/10, len2-1)
}
// num Before , There is a beginning !
// It's about 0 Under the circumstances ,num It's the number
// num 76217
// 10000
// 6217
// 1000
// len
func process(num int, offset int, len2 int) int {
if len2 == 0 {
return 1
}
first := num / offset
return twoSelectOne(first < 4, first, (first-1))*arr[len2] + process(num%offset, offset/10, len2-1)
}
// num The decimal digits of
// num = 7653210
// 7
func decimalLength(num int) int {
len2 := 0
for num != 0 {
len2++
num /= 10
}
return len2
}
// len = 6
// 100000
// len = 4
// 1000
// 3452168
// 1000000
// 3
func offset(len2 int) int {
offset := 1
for i := 1; i < len2; i++ {
offset *= 10
}
return offset
}
// After the lecture, I thought of the students' messages in class
// Suddenly realized , The essence of this problem is a 9 The base number is converted to 10 Binary number
// But fortunately, the solution in class has practical significance , This is the way to solve , A lot of questions are like this
// And the time complexity and "9 The base number is converted to 10 Binary number " How to do it , The time complexity is zero O(lg N)
// however "9 The base number is converted to 10 Binary number " There is no doubt that the optimal solution
func notContains4Nums3(num int) int {
if num <= 0 {
return 0
}
ans := 0
for base, cur := 1, 0; num != 0; num, base = num/10, base*9 {
cur = num % 10
ans += twoSelectOne(cur < 4, cur, cur-1) * base
}
return ans
}
func twoSelectOne(c bool, a int, b int) int {
if c {
return a
} else {
return b
}
}The results are as follows :
边栏推荐
- Bi-sql insert into
- How does ECS publish websites? What software tools are needed?
- Collagenase -- four types of crude collagenase from Worthington
- How to create an FTP server on the ECS? Is it safe to create an FTP server on the ECS?
- How does the VPS server upload data? Is the VPS server free to use?
- Integration of Alibaba cloud SMS services and reasons for illegal message signing
- Confluence data center version is nearing its lifecycle
- How to open the port of ECS what are the precautions for using ECS
- Many regulations come into effect today! The main responsibility of network security will be further implemented
- Precautions for online education and training industry filing
猜你喜欢

少儿编程课程改革后的培养方式

少儿编程教育在特定场景中的普及作用

Leetcode question brushing (question 3) - the longest substring without repeated characters

Apipost interface assertion details

MySQL - SQL execution process

SAP mts/ato/mto/eto topic 10: ETO mode q+ empty mode unvalued inventory policy customization

让孩子们学习Steam 教育的应用精髓

What is the new generation cloud computing architecture cipu of Alibaba cloud?

Are you ready for the exam preparation strategy of level II cost engineer in 2022?

阿里云混合云首席架构师张晓丹:政企混合云技术架构的演进和发展
随机推荐
Elfk service setup
CTF learning notes 18:iwesec file upload vulnerability-03-content-type filtering bypass
Deep learning common optimizer summary
How to select a suitable optical fiber tester
What is an ECS? What is the difference between ECs and traditional servers?
『应急响应实践』LogParser日志分析实践
Bi-sql - Select
让孩子们学习Steam 教育的应用精髓
Introduction to gradient descent method - black horse programmer machine learning handout
Specificity and correlation of Worthington deoxyribonuclease I
4G industrial VPN router
What does VPS server mean? What is the difference between a VPS server and an ECS?
Physicochemical properties and specificity of Worthington trypsin
梯度下降法介绍-黑马程序员机器学习讲义
查找GBase 8c数据库当前索引?
How to restart the ECS? What are the differences between ECS restart and normal computers?
How to build an ECS and how to control the server through the local host
Analysis on the subjective enthusiasm of post-90s makers' Education
MySQL - SQL execution process
Qiming cloud sharing: tips on esp32c3 simple IO and serial port