当前位置:网站首页>Leetcode T29: 两数相除
Leetcode T29: 两数相除
2022-07-01 08:08:00 【范谦之】
题目描述
给定两个整数,被除数 dividend 和除数 divisor。将两数相除,要求不使用乘法、除法和 mod 运算符。
返回被除数 dividend 除以除数 divisor 得到的商。
整数除法的结果应当截去(truncate)其小数部分,例如:truncate(8.345) = 8 以及 truncate(-2.7335) = -2
示例 1:
输入: dividend = 10, divisor = 3
输出: 3
解释: 10/3 = truncate(3.33333…) = truncate(3) = 3
示例 2:
输入: dividend = 7, divisor = -3
输出: -2
解释: 7/-3 = truncate(-2.33333…) = -2
提示
- 被除数和除数均为 32 位有符号整数。
- 除数不为 0。
- 假设我们的环境只能存储 32 位有符号整数,其数值范围是 [ − 2 31 , 2 31 − 1 ] [−2^{31}, 2^{31} − 1] [−231,231−1]。本题中,如果除法结果溢出,则返回 2^{31} − 1。
思路
由于题目中不能使用乘法、除法和取余运算,可以考虑利用加减法计算。
例如,考虑100/7=14… …2,可以使用100对7累减,直到为负数。但是,这样效率太低,可以考虑一下减去多个7(比如 2 i 2^i 2i 个7),来提高计算效率。
可以采取如下做法:
100 − 2 3 ∗ 7 = 100 − 56 = 44 100-2^3*7=100-56=44 100−23∗7=100−56=44,
44 − 2 2 ∗ 7 = 44 − 28 = 16 44-2^2*7=44-28=16 44−22∗7=44−28=16,
16 − 2 1 ∗ 7 = 2 16-2^1*7=2 16−21∗7=2,
由于 2 < 7 2<7 2<7,所以最后的余数是2,除数为 2 1 + 2 2 + 2 3 = 14 2^1+2^2+2^3=14 21+22+23=14.
但是,要考虑特殊情况:
- 被除数为0,直接输出0.
- 被除数为 − 2 31 -2^{31} −231,而除数是-1,那么,根据题意,应返回 2 31 − 1 2^{31}-1 231−1
代码
public int divide(int dividend, int divisor) {
if (dividend == 0) {
return 0;
}
if (dividend == Integer.MIN_VALUE && divisor == -1) {
return Integer.MAX_VALUE;
}
boolean negative;
negative = (dividend^divisor) < 0;
long t = Math.abs((long)dividend);
long d = Math.abs((long)divisor);
int result = 0;
for(int i = 31; i >= 0; i--) {
if( (d<<i) <= t ) {
result += (1<<i);
t -= d<<i;
}
}
return negative ? -result : result;//符号相异取反
}
边栏推荐
- Two expressions of string
- ContentType所有类型对比
- 图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证
- [question brushing] character statistics [0]
- Programmer's regimen
- 5大组合拳,解决校园6大难题,护航教育信息化建设
- XX攻击——反射型 XSS 攻击劫持用户浏览器
- Differential: definition of total differential, partial derivative, gradient
- 手工挖XSS漏洞
- Hijacking a user's browser with beef
猜你喜欢
![[untitled]](/img/b9/6922875009c2d29224a26ed2a22b01.jpg)
[untitled]
![[getting started] extract non repeating integers](/img/88/3e96df88e980bd98ac112b18a8678c.png)
[getting started] extract non repeating integers

How to troubleshoot SharePoint online map network drive failure?

Using settoolkit to forge sites to steal user information

CPU设计实战-第四章实践任务一简单CPU参考设计调试
![[redis] it takes you through redis installation and connection at one go](/img/ca/89cb18f0eeb835f021d6a2489681a1.png)
[redis] it takes you through redis installation and connection at one go

On several key issues of digital transformation
![[dynamic planning] p1020 missile interception (variant of the longest increasing subsequence)](/img/3e/75a1152f9cdf63c6779fdadec702a0.jpg)
[dynamic planning] p1020 missile interception (variant of the longest increasing subsequence)

【入门】截取字符串
![[staff] key number (key number identification position | key number marking list | a major key identification principle | F, C, G position marking ascending | F major key identification principle | B](/img/48/e98d01830867baa742574e1b6e1096.jpg)
[staff] key number (key number identification position | key number marking list | a major key identification principle | F, C, G position marking ascending | F major key identification principle | B
随机推荐
web254
【力扣10天SQL入门】Day9 控制流
Deep learning systematic learning
How to prevent the other party from saying that he has no money after winning the lawsuit?
力扣每日一题-第32天-1822.数组元素积的符号
Day5: scanner object, next() and nextline(), sequential structure, selection structure, circular structure
SQL number injection and character injection
Why are some Wills made by husband and wife invalid
[staff] high and low octave mark (the notes in the high octave mark | mark range are increased by one octave as a whole | low octave mark | mark range are decreased by one octave as a whole)
Find the nearest n-th power of 2
【力扣10天SQL入门】Day10 控制流
【批处理DOS-CMD命令-汇总和小结】-Cmd窗口中常用操作符(<、<<、&<、>、>>、&>、&、&&、||、|、()、;、@)
Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system
使用 setoolkit 伪造站点窃取用户信息
Differential: definition of total differential, partial derivative, gradient
【入门】取近似值
如何使用layui将数据库中的数据以表格的形式展现出来
Cyclic neural network
Provincial election + noi Part III tree problems
防“活化”照片蒙混过关,数据宝“活体检测+人脸识别”让刷脸更安全