当前位置:网站首页>[algorithm] sword finger offer2 golang interview question 1: integer division
[algorithm] sword finger offer2 golang interview question 1: integer division
2022-07-06 12:50:00 【Deng Jiawen jarvan】
[ Algorithm ] The finger of the sword offer2 golang Interview questions 1: Integer division
subject 1:
Given two integers a and b , Find the quotient of their division a/b , It is required that the multiplication sign... Shall not be used ‘*’、 devide ‘/’ And the remainder symbol ‘%’ .
Be careful :
The result of integer division should be truncated (truncate) Its decimal part , for example :truncate(8.345) = 8 as well as truncate(-2.7335) = -2
Suppose our environment can only store 32 Bit signed integer , The value range is [−231, 231−1]. In this question , If the division result overflows , Then return to 231 − 1
Example 1:
Input :a = 15, b = 2
Output :7
explain :15/2 = truncate(7.5) = 7
Example 2:
Input :a = 7, b = -3
Output :-2
explain :7/-3 = truncate(-2.33333..) = -2
Example 3:
Input :a = 0, b = 1
Output :0
Example 4:
Input :a = 1, b = 1
Output :1
Tips :
-231 <= a, b <= 231 - 1
b != 0
Ideas 1:
The first idea is to use speech instead of division
such as 5/2 Can keep 5-2=3> 0; res ++;
3-2=1 > 0; res++
But encountered 100000/2 It's too complicated , We try to use exponential increments , such as 5/2
15 - 2 = 13> 2; res = 1
15- 4 = 11> 2 ; res = 2
15- 8 > 7 >2; res = 4
15 - 16 = -1 < 2; res = 8, This value gives up , res = 4, And add... To the total return value 4, resTotal += res, Union divisor =7, From the beginning, the index increases
7-2 = 5 > 2 ; res = 1
7-4 = 3 > 2 ; res = 2
7 -8 = -1 < 2; res =4, This value gives up , res = 2, And add... To the total return value 2, …
func myDividefunc(a int, b int) int {
// The return value will overflow +2^32
// Wrong input : Divisor is 0
if (a == math.MinInt32 && b == -1) || b == 0 {
return math.MaxInt32
}
// The recording time is positive
negative := 2
if a > 0 {
negative--
a = -a
}
if b > 0 {
negative--
b = -b
}
// Negative number calculation result
res := divideCore(a, b)
if negative == 1 {
res = -res
}
return res
}
func divideCore(a int, b int) int {
res := 0
// -7 <= -3
for a <= b {
resAdd := 1
b2 := b
for {
// Double every time
if a < (b2 + b2) {
// The result doubled each time
resAdd += resAdd
b2 += b2
} else {
break
}
}
a -= b2
res += resAdd
}
return res
}
Code
func divide(a int, b int) int {
// The return value will overflow +2^32
// Wrong input : Divisor is 0
if (a == math.MinInt32 && b == -1) || b == 0 {
return math.MaxInt32
}
// The recording time is positive
negative := 2
if a > 0 {
negative--
a = -a
}
if b > 0 {
negative--
b = -b
}
// Negative number calculation result
res := divideCore(a, b)
if negative == 1 {
res = -res
}
return res
}
//-7/-3
// Calculate in exponential increments
//-7 < -3, 1
//-7 < -6 2
// -7 > -12 3
//-13/3
func divideCore(a int, b int) int {
res := 0
// -7 <= -3
for a <= b {
resAdd := 1
b2 := b
for {
// Double every time
if a < (b2+b2) {
// The result doubled each time
resAdd += resAdd
b2 += b2
}else {
break
}
}
a -= b2
res += resAdd
}
return res
}
test

边栏推荐
- Database table splitting strategy
- Fabrication d'un sac à dos simple fairygui
- Unity场景跳转及退出
- [Chongqing Guangdong education] reference materials for regional analysis and planning of Pingdingshan University
- Matlab读取GNSS 观测值o文件代码示例
- FairyGUI复选框与进度条的组合使用
- MySQL shutdown is slow
- [算法] 剑指offer2 golang 面试题5:单词长度的最大乘积
- 微信小程序开发心得
- Fairygui character status Popup
猜你喜欢

Unity scene jump and exit

Matlab读取GNSS 观测值o文件代码示例

(1) Introduction Guide to R language - the first step of data analysis

In 2020, the average salary of IT industry exceeded 170000, ranking first

(core focus of software engineering review) Chapter V detailed design exercises
![[Chongqing Guangdong education] Shandong University College Physics reference materials](/img/56/4ac44729c3e480a4f779d6a890363a.jpg)
[Chongqing Guangdong education] Shandong University College Physics reference materials
![[算法] 剑指offer2 golang 面试题9:乘积小于k的子数组](/img/65/fc3fb5a217a3b44f506b695af53e2c.png)
[算法] 剑指offer2 golang 面试题9:乘积小于k的子数组

FairyGUI复选框与进度条的组合使用
![[算法] 剑指offer2 golang 面试题6:排序数组中的两个数字之和](/img/d5/4bda133498f71ae9fd7a64c6cba8f0.png)
[算法] 剑指offer2 golang 面试题6:排序数组中的两个数字之和

KF UD分解之UD分解基础篇【1】
随机推荐
2021.11.10汇编考试
[offer29] sorted circular linked list
[算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组
Containers and Devops: container based Devops delivery pipeline
Talking about the startup of Oracle Database
平衡二叉树详解 通俗易懂
Easy to use shortcut keys in idea
燕山大学校园网自动登录问题解决方案
Naive Bayesian theory derivation
Mysql database reports an error: row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT=DY
Devops' future: six trends in 2022 and beyond
(课设第一套)1-5 317号子任务 (100 分)(Dijkstra:重边自环)
Minio file download problem - inputstream:closed
FairyGUI循环列表
【干货】提升RTK模糊度固定率的建议之周跳探测
rtklib单点定位spp使用抗差估计遇到的问题及解决
Agile development helps me
(1) Introduction Guide to R language - the first step of data analysis
Programming homework: educational administration management system (C language)
Excel导入,导出功能实现