当前位置:网站首页>[算法] 剑指offer2 golang 面试题1:整数除法
[算法] 剑指offer2 golang 面试题1:整数除法
2022-07-06 09:18:00 【邓嘉文Jarvan】
[算法] 剑指offer2 golang 面试题1:整数除法
题目1:
给定两个整数 a 和 b ,求它们的除法的商 a/b ,要求不得使用乘号 ‘*’、除号 ‘/’ 以及求余符号 ‘%’ 。
注意:
整数除法的结果应当截去(truncate)其小数部分,例如:truncate(8.345) = 8 以及 truncate(-2.7335) = -2
假设我们的环境只能存储 32 位有符号整数,其数值范围是 [−231, 231−1]。本题中,如果除法结果溢出,则返回 231 − 1
示例 1:
输入:a = 15, b = 2
输出:7
解释:15/2 = truncate(7.5) = 7
示例 2:
输入:a = 7, b = -3
输出:-2
解释:7/-3 = truncate(-2.33333..) = -2
示例 3:
输入:a = 0, b = 1
输出:0
示例 4:
输入:a = 1, b = 1
输出:1
提示:
-231 <= a, b <= 231 - 1
b != 0
思路1:
首先的思路是使用讲话代替除法
比如 5/2 可以不断 5-2=3> 0; res ++;
3-2=1 > 0; res++
但是遇到 100000/2 复杂度太高了,我们尝试使用指数级递增, 比如 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, 这个值放弃, res = 4,并在总返回值上加 4, resTotal += res, 并令除数=7, 从头开始指数递增
7-2 = 5 > 2 ; res = 1
7-4 = 3 > 2 ; res = 2
7 -8 = -1 < 2; res =4, 这个值放弃, res = 2,并在总返回值上加 2, …
func myDividefunc(a int, b int) int {
//返回值会溢出 +2^32
//错误输入: 除数为0
if (a == math.MinInt32 && b == -1) || b == 0 {
return math.MaxInt32
}
//记录时候是正数
negative := 2
if a > 0 {
negative--
a = -a
}
if b > 0 {
negative--
b = -b
}
// 负数计算结果
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 {
//每次翻倍
if a < (b2 + b2) {
//结果每次翻倍
resAdd += resAdd
b2 += b2
} else {
break
}
}
a -= b2
res += resAdd
}
return res
}
代码
func divide(a int, b int) int {
//返回值会溢出 +2^32
//错误输入: 除数为0
if (a == math.MinInt32 && b == -1) || b == 0 {
return math.MaxInt32
}
//记录时候是正数
negative := 2
if a > 0 {
negative--
a = -a
}
if b > 0 {
negative--
b = -b
}
// 负数计算结果
res := divideCore(a, b)
if negative == 1 {
res = -res
}
return res
}
//-7/-3
//使用指数级递增的方式计算
//-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 {
//每次翻倍
if a < (b2+b2) {
//结果每次翻倍
resAdd += resAdd
b2 += b2
}else {
break
}
}
a -= b2
res += resAdd
}
return res
}
测试
边栏推荐
- What are the functions and features of helm or terrain
- Unity3D,阿里云服务器,平台配置
- 微信小程序开发心得
- Theoretical derivation of support vector machine
- How to add music playback function to Arduino project
- GNSS定位精度指标计算
- Single chip Bluetooth wireless burning
- Mysql database index
- (the first set of course design) 1-4 message passing interface (100 points) (simulation: thread)
- Fairygui joystick
猜你喜欢
SVN更新后不出现红色感叹号
What are the advantages of using SQL in Excel VBA
Mixed use of fairygui button dynamics
341. Flatten nested list iterator
抗差估计在rtklib的pntpos函数(标准单点定位spp)中的c代码实现
Fairygui gain buff value change display
FairyGUI按钮动效的混用
(5) Introduction to R language bioinformatics -- ORF and sequence analysis
Affichage du changement de valeur du Buff de gain de l'interface graphique de défaillance
Naive Bayesian theory derivation
随机推荐
Conditional probability
【干货】提升RTK模糊度固定率的建议之周跳探测
音乐播放(Toggle && PlayerPrefs)
MySQL time, time zone, auto fill 0
Excel导入,导出功能实现
[899] ordered queue
There is no red exclamation mark after SVN update
Agile development helps me
Mixed use of fairygui button dynamics
[Clickhouse kernel principle graphic explanation] about the collaborative work of partitioning, indexing, marking and compressed data
Special palindromes of daily practice of Blue Bridge Cup
[offer29] sorted circular linked list
FairyGUI增益BUFF數值改變的顯示
Unity scene jump and exit
KF UD分解之UD分解基础篇【1】
Get the position of the nth occurrence of the string
服务未正常关闭导致端口被占用
Solution to the problem of automatic login in Yanshan University Campus Network
Derivation of logistic regression theory
[leetcode19] delete the penultimate node in the linked list