当前位置:网站首页>[算法] 剑指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
}
测试

边栏推荐
- Prove the time complexity of heap sorting
- (the first set of course design) sub task 1-5 317 (100 points) (dijkstra: heavy edge self loop)
- [Nodejs] 20. Koa2 onion ring model ----- code demonstration
- (4) Data visualization of R language -- matrix chart, histogram, pie chart, scatter chart, linear regression and strip chart
- By v$rman_ backup_ job_ Oracle "bug" caused by details
- Lean product development - Lean Software Development & lean product development
- Liste des boucles de l'interface graphique de défaillance
- NRF24L01故障排查
- Affichage du changement de valeur du Buff de gain de l'interface graphique de défaillance
- RTKLIB: demo5 b34f.1 vs b33
猜你喜欢

(3) Introduction to bioinformatics of R language - function, data Frame, simple DNA reading and analysis

FairyGUI增益BUFF数值改变的显示

idea问题记录

Excel导入,导出功能实现

(5) Introduction to R language bioinformatics -- ORF and sequence analysis

Single chip Bluetooth wireless burning

Prove the time complexity of heap sorting

音乐播放(Toggle && PlayerPrefs)

Teach you to release a DeNO module hand in hand

Combination of fairygui check box and progress bar
随机推荐
(core focus of software engineering review) Chapter V detailed design exercises
Particle system for introduction to unity3d Foundation (attribute introduction + case production of flame particle system)
[offer29] sorted circular linked list
Design and implementation of general interface open platform - (39) simple and crude implementation of API services
FairyGUI简单背包的制作
[Chongqing Guangdong education] reference materials for regional analysis and planning of Pingdingshan University
Derivation of logistic regression theory
SVN更新后不出现红色感叹号
FairyGUI复选框与进度条的组合使用
dosbox第一次使用
First use of dosbox
InnoDB dirty page refresh mechanism checkpoint in MySQL
[算法] 剑指offer2 golang 面试题13:二维子矩阵的数字之和
基于rtklib源码进行片上移植的思路分享
编译原理:源程序的预处理及词法分析程序的设计与实现(含代码)
(四)R语言的数据可视化——矩阵图、柱状图、饼图、散点图与线性回归、带状图
Compile GDAL source code with nmake (win10, vs2022)
It has been solved by personal practice: MySQL row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT
Get the position of the nth occurrence of the string
【rtklib】在rtk下使用抗差自适应卡尔曼滤波初步实践