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

边栏推荐
- [offer78]合并多个有序链表
- Particle system for introduction to unity3d Foundation (attribute introduction + case production of flame particle system)
- Database course design: college educational administration management system (including code)
- 音乐播放(Toggle && PlayerPrefs)
- 【RTKLIB 2.4.3 b34 】版本更新简介一
- [Nodejs] 20. Koa2 onion ring model ----- code demonstration
- (课设第一套)1-4 消息传递接口 (100 分)(模拟:线程)
- [offer29] sorted circular linked list
- Detailed explanation of truncate usage
- 2021.11.10汇编考试
猜你喜欢

第一人称视角的角色移动

【干货】提升RTK模糊度固定率的建议之周跳探测
![[算法] 剑指offer2 golang 面试题13:二维子矩阵的数字之和](/img/17/e7c9bfa867030af97eb66a7932c7e3.png)
[算法] 剑指offer2 golang 面试题13:二维子矩阵的数字之和

First use of dosbox

Liste des boucles de l'interface graphique de défaillance

RTKLIB: demo5 b34f.1 vs b33

Compilation principle: preprocessing of source program and design and implementation of lexical analysis program (including code)

Affichage du changement de valeur du Buff de gain de l'interface graphique de défaillance

(4) Data visualization of R language -- matrix chart, histogram, pie chart, scatter chart, linear regression and strip chart

NovAtel 板卡OEM617D配置步骤记录
随机推荐
[offer9]用两个栈实现队列
What is the maximum length of MySQL varchar field
Intermediate use tutorial of postman [environment variables, test scripts, assertions, interface documents, etc.]
1041 be unique (20 points (s)) (hash: find the first number that occurs once)
Comparative analysis of the execution efficiency of MySQL 5.7 statistical table records
isEmpty 和 isBlank 的用法区别
Theoretical derivation of support vector machine
Latex learning
FairyGUI人物状态弹窗
Lean product development - Lean Software Development & lean product development
By v$rman_ backup_ job_ Oracle "bug" caused by details
idea中导包方法
如何给Arduino项目添加音乐播放功能
Prove the time complexity of heap sorting
[offer78] merge multiple ordered linked lists
SSD technical features
FairyGUI摇杆
基于rtklib源码进行片上移植的思路分享
[offer29] sorted circular linked list
Meanings and differences of PV, UV, IP, VV, CV