当前位置:网站首页>[算法] 剑指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
}
测试
边栏推荐
- Meanings and differences of PV, UV, IP, VV, CV
- Lock wait timeout exceeded try restarting transaction
- Unity3D制作注册登录界面,并实现场景跳转
- [offer9]用两个栈实现队列
- VLSM variable length subnet mask partition tips
- Introduction to the daily practice column of the Blue Bridge Cup
- First use of dosbox
- [offer9] implement queues with two stacks
- 1041 Be Unique (20 point(s))(哈希:找第一个出现一次的数)
- Detailed explanation of truncate usage
猜你喜欢
Gravure sans fil Bluetooth sur micro - ordinateur à puce unique
抗差估计在rtklib的pntpos函数(标准单点定位spp)中的c代码实现
Esp8266 connect onenet (old mqtt mode)
NRF24L01故障排查
NovAtel 板卡OEM617D配置步骤记录
Mixed use of fairygui button dynamics
Derivation of logistic regression theory
平衡二叉树详解 通俗易懂
Excel导入,导出功能实现
rtklib单点定位spp使用抗差估计遇到的问题及解决
随机推荐
(the first set of course design) sub task 1-5 317 (100 points) (dijkstra: heavy edge self loop)
ORA-02030: can only select from fixed tables/views
[offer78] merge multiple ordered linked lists
Gateway fails to route according to the service name, and reports an error service unavailable, status=503
(四)R语言的数据可视化——矩阵图、柱状图、饼图、散点图与线性回归、带状图
微信小程序开发心得
Excel导入,导出功能实现
How to add music playback function to Arduino project
[leetcode622]设计循环队列
KF UD分解之UD分解基础篇【1】
GNSS定位精度指标计算
Fairygui loop list
Get the position of the nth occurrence of the string
基本Dos命令
Containers and Devops: container based Devops delivery pipeline
What are the functions and features of helm or terrain
There is no red exclamation mark after SVN update
Flink late data processing (3)
FairyGUI条子家族(滚动条,滑动条,进度条)
How to reduce the shutdown time of InnoDB database?