当前位置:网站首页>[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

边栏推荐
- [algorithme] swordfinger offer2 golang question d'entrevue 2: addition binaire
- Combination of fairygui check box and progress bar
- Compile GDAL source code with nmake (win10, vs2022)
- [算法] 剑指offer2 golang 面试题4:只出现一次的数字
- Lock wait timeout exceeded try restarting transaction
- [leetcode19] delete the penultimate node in the linked list
- 服务未正常关闭导致端口被占用
- Database table splitting strategy
- [offer29] sorted circular linked list
- MySQL shutdown is slow
猜你喜欢

Fabrication d'un sac à dos simple fairygui

Theoretical derivation of support vector machine

Gravure sans fil Bluetooth sur micro - ordinateur à puce unique

Esp8266 connect onenet (old mqtt mode)

Lock wait timeout exceeded try restarting transaction

Fairygui joystick

FairyGUI条子家族(滚动条,滑动条,进度条)

【GNSS数据处理】赫尔默特(helmert)方差分量估计解析及代码实现

Latex learning

idea中导包方法
随机推荐
SVN更新后不出现红色感叹号
Knowledge system of digital IT practitioners | software development methods -- agile
FairyGUI简单背包的制作
(3) Introduction to bioinformatics of R language - function, data Frame, simple DNA reading and analysis
Affichage du changement de valeur du Buff de gain de l'interface graphique de défaillance
Single chip Bluetooth wireless burning
[leetcode19] delete the penultimate node in the linked list
[算法] 剑指offer2 golang 面试题9:乘积小于k的子数组
[offer9]用两个栈实现队列
[Clickhouse kernel principle graphic explanation] about the collaborative work of partitioning, indexing, marking and compressed data
Office prompts that your license is not genuine pop-up box solution
1041 Be Unique (20 point(s))(哈希:找第一个出现一次的数)
wsl常用命令
[算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组
Easy to use shortcut keys in idea
Office提示您的许可证不是正版弹框解决
[算法] 剑指offer2 golang 面试题5:单词长度的最大乘积
Servlet
FairyGUI循环列表
idea中好用的快捷键