当前位置:网站首页>2021-05-03: given a non negative integer num, how to avoid circular statements,
2021-05-03: given a non negative integer num, how to avoid circular statements,
2022-06-24 15:55:00 【Fuda scaffold constructor's daily question】
2021-05-03: Given a nonnegative integer num, How to avoid circular statements , return >=num, And leave num Current ,2 To the power of .
Fuda answer 2021-05-03:
32 An integer ,N=32.
1. Nonnegative integers use int Express . The time complexity is logN.
The binary form of an integer minus one ,1 All the numbers on the right become 1, Finally add 1 Is the result that needs to be returned .
2. Nonnegative integers use float64 Express . Floating point numbers implicitly use log( Integers ) Result , So the complexity is O(1). This method is a bit sneaky , Because the title is an integer , And here we use float64, It's not an integer , But the idea is strange , Therefore, the .
Floating point numbers = Sign bit + Order code + mantissa . When mantissa is not 0 When , The mantissa becomes 0, Order code +1, This is the memory result of the floating point number to be returned ; When mantissa is 0 When , The current floating-point number is the result to be returned .
The code to use golang To write . The code is as follows :
package main import ( "fmt" "math" ) func main() { for i := 1; i <= 129; i++ { fmt.Println(i, tableSizeFor1(i), tableSizeFor2(float64(i))) } } // It is known that n Positive number // Return greater than or equal to , And closest to n Of ,2 Value to the power of func tableSizeFor1(n int) int { n-- n |= n >> 1 n |= n >> 2 n |= n >> 4 n |= n >> 8 n |= n >> 16 return twoSelectOne(n < 0, 1, n+1) } func twoSelectOne(condition bool, a int, b int) int { if condition { return a } else { return b } } func tableSizeFor2(a float64) float64 { _, exp, frac := fromFloat64(a) if frac != 0 { exp++ frac = 0 } return getFloat64(0, exp, frac) } // Find the sign bit according to the floating point number , Order code , mantissa func fromFloat64(f float64) (uint64, uint64, uint64) { u := math.Float64bits(f) return u >> 63, u >> 52 & 0b00000111_11111111, u & 0b00000000_00001111_11111111_11111111_11111111_11111111_11111111_11111111 } // According to the sign bit , Order code , Mantissa floating point number func getFloat64(s uint64, exp uint64, frac uint64) float64 { s = s << 63 exp = exp & 0b00000111_11111111 << 52 frac = frac & 0b00000000_00001111_11111111_11111111_11111111_11111111_11111111_11111111 return math.Float64frombits(s | exp | frac) }
The results are as follows :
***
边栏推荐
- Flink Kubernetes Application部署
- Istio practical skill: hide the automatically added server header
- [application recommendation] the hands-on experience and model selection suggestions of apifox & apipost in the recent fire
- 设备通过国标GB28181接入EasyCVR平台,出现断流情况该如何解决?
- 推荐几款超级实用的数据分析利器
- The decline of China's product managers: starting from the nostalgia for jobs
- Vim编辑器的最常用的用法
- CAP:多重注意力机制,有趣的细粒度分类方案 | AAAI 2021
- MySQL development specification
- Implement Domain Driven Design - use ABP framework - domain logic & application logic
猜你喜欢
Jenkins 镜像无法更新插件中心的3种解决方法
微信公众号调试与Natapp环境搭建
Most common usage of vim editor
如何扩展aws主机上的磁盘空间
[C language questions -- leetcode 12 questions] take you off and fly into the garbage
一文详解JackSon配置信息
MySQL binlog
FreeRTOS新建任务不执行问题解决办法
Mongodb introductory practical tutorial: learning summary directory
[my advanced OpenGL learning journey] learning notes of OpenGL coordinate system
随机推荐
Golang+redis reentrant lock
Nature publishes significant progress in quantum computing: the first quantum integrated circuit implementation in history
Flink kubernetes application deployment
Improving the classification of motor imagery by combining EEG and MEG signals in BCI
在Gradle 中对Junit5 测试框架引用
2021-04-25: given an array arr and a positive number m, the
Build go command line program tool chain
clang: warning: argument unused during compilation: ‘-no-pie‘ [-Wunused-command-line-argument]
Design of CAN bus controller based on FPGA (Part 2)
Ascinema with asciicast2gif for efficient command line terminal recording
Poor remote code execution in Alien Swarm
VNC Viewer方式的远程连接树莓派
The 30 pictures bring the network protocol layer by layer to life. It's really fragrant!
Recommend several super practical data analysis tools
How to expand disk space on AWS host
Istio FAQ: region awareness does not take effect
[log service CLS] Tencent cloud log4j/logback log collection best practices
存在安全隐患 部分冒险家混动版将召回
MySQL development specification
leetcode 139. Word Break 單詞拆分(中等)