当前位置:网站首页>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;//符号相异取反
}
边栏推荐
- 力扣每日一题-第31天-202.快乐数
- ContentType所有类型对比
- Lm08 mesh series mesh inversion (fine)
- P4 installation bmv2 detailed tutorial
- slice扩容机制分析
- php laravel微信支付
- Two expressions of string
- Li Kou daily question - day 31 -1502 Judge whether an arithmetic sequence can be formed
- AArdio - 【问题】bass库回调时内存增长的问题
- Differential: definition of total differential, partial derivative, gradient
猜你喜欢

Access report realizes subtotal function

【无标题】

Android screen adaptation (using constraintlayout), kotlin array sorting

The Windows C disk is full

Office365 - how to use stream app to watch offline files at any time

Erreur de hauteur du clavier souple

Principle and process of embossing

Microsoft stream - how to modify video subtitles

使用beef劫持用戶瀏覽器

window c盘满了
随机推荐
How to prevent the other party from saying that he has no money after winning the lawsuit?
How to check ad user information?
SharePoint - how to quickly check whether SharePoint is standard or enterprise edition?
Li Kou daily question - Day 32 -1822 Symbol of array element product
LM08丨网格系列之网格反转(精)
PostgreSQL source code learning (26) -- windows vscode remote debugging PostgreSQL on Linux
php laravel微信支付
XX attack - reflective XSS attack hijacking user browser
EDA open source simulation tool verilator beginner 6: debugging examples
Provincial election + noi part I dynamic planning DP
How can beginners correctly understand Google's official suggested architectural principles (questions?)
Latex table
Lm08 mesh series mesh inversion (fine)
P4 安装bmv2 详细教程
[dynamic planning] p1020 missile interception (variant of the longest increasing subsequence)
Serial port oscilloscope software ns-scope
web254
【入门】输入整型数组和排序标识,对其元素按照升序或降序进行排序
Gdip - hatchBrush图案表
Day5: scanner object, next() and nextline(), sequential structure, selection structure, circular structure