当前位置:网站首页>[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
边栏推荐
猜你喜欢
Single chip Bluetooth wireless burning
Fairygui gain buff value change display
NovAtel 板卡OEM617D配置步骤记录
平衡二叉树详解 通俗易懂
Vulnhub target: hacknos_ PLAYER V1.1
Fairygui loop list
Compilation principle: preprocessing of source program and design and implementation of lexical analysis program (including code)
[算法] 剑指offer2 golang 面试题9:乘积小于k的子数组
编译原理:源程序的预处理及词法分析程序的设计与实现(含代码)
Derivation of logistic regression theory
随机推荐
Compilation principle: preprocessing of source program and design and implementation of lexical analysis program (including code)
Fairygui joystick
[Nodejs] 20. Koa2 onion ring model ----- code demonstration
【无标题】
[899] ordered queue
3月15号 Go 1.18 正式版发布 了解最新特色以及使用方法
Talking about the startup of Oracle Database
rtklib单点定位spp使用抗差估计遇到的问题及解决
Theoretical derivation of support vector machine
Guided package method in idea
2021.11.10汇编考试
Get the position of the nth occurrence of the string
[算法] 剑指offer2 golang 面试题4:只出现一次的数字
Fairygui character status Popup
Detailed explanation of truncate usage
FairyGUI循环列表
Excel导入,导出功能实现
單片機藍牙無線燒錄
Agile development helps me
[Chongqing Guangdong education] Shandong University College Physics reference materials