当前位置:网站首页>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;//符号相异取反
}
边栏推荐
- 程序员养生宝典
- Erreur de hauteur du clavier souple
- [dynamic planning] p1020 missile interception (variant of the longest increasing subsequence)
- Adding color blocks to Seaborn clustermap matrix
- Access报表实现小计功能
- The Windows C disk is full
- [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)
- Gdip - hatchBrush图案表
- How to check ad user information?
- 使用threejs简单Web3D效果
猜你喜欢

Microsoft stream - how to modify video subtitles

Learn the knowledge you need to know about the communication protocol I2C bus
![[website architecture] solve 90% of distributed transactions in one move, and introduce the working principles and application scenarios of database transactions and distributed transactions](/img/2c/07d729d49b1d74553decac4588074e.png)
[website architecture] solve 90% of distributed transactions in one move, and introduce the working principles and application scenarios of database transactions and distributed transactions

How outlook puts together messages with the same discussion

【无标题】

使用 setoolkit 伪造站点窃取用户信息

Gdip - hatchbrush pattern table

谈谈数字化转型的几个关键问题
![[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)](/img/ff/ebd936eaa6e57b1eabb691b0642957.jpg)
[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)

P4 安装bmv2 详细教程
随机推荐
【入门】输入整型数组和排序标识,对其元素按照升序或降序进行排序
String coordinates of number to excel
The difference between interceptors and filters
初学者如何正确理解google官方建议架构原则(疑问?)
How to get a SharePoint online site created using the office365 group template
SQL number injection and character injection
力扣每日一题-第31天-202.快乐数
[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)
postgresql源码学习(26)—— Windows vscode远程调试Linux上的postgresql
sqlalchemy创建MySQL_Table
Insufficient executors to build thread pool
Teach you how to apply for domestic trademark online step by step
Deep learning systematic learning
go通用动态重试机制解决方案的实现与封装
uni 热更新
谈谈数字化转型的几个关键问题
Aardio - [problem] the problem of memory growth during the callback of bass Library
web254
Android screen adaptation (using constraintlayout), kotlin array sorting
[batch DOS CMD summary] extension variables - delay variables CMD /v:on, CMD /v:off, SETLOCAL enabledelayedexpansion, disabledelayedexpansion