两个数相除,禁止使用乘、除及求模运算
两个数相除,禁止使用乘、除及求模运算
题目
给定两个整数,被除数 dividend 和除数 divisor。将两数相除,要求不使用乘法、除法和 mod 运算符。
返回被除数 dividend 除以除数 divisor 得到的商。
整数除法的结果应当截去(truncate)其小数部分,例如:truncate(8.345) = 8 以及 truncate(-2.7335) = -2
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/divide-two-integers
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题方案
- 二分查找思想
class Solution {
public int divide(int dividend, int divisor) {
// 考虑被除数为最小值的情况
if (dividend == Integer.MIN_VALUE) {
if (divisor == 1) {
return Integer.MIN_VALUE;
}
if (divisor == -1) {
return Integer.MAX_VALUE;
}
}
// 考虑除数为最小值的情况
if (divisor == Integer.MIN_VALUE) {
return dividend == Integer.MIN_VALUE ? 1 : 0;
}
// 考虑被除数为 0 的情况
if (dividend == 0) {
return 0;
}
// 一般情况,使用二分查找
// 将所有的正数取相反数,这样就只需要考虑一种情况
boolean rev = false;
if (dividend > 0) {
dividend = -dividend;
rev = !rev;
}
if (divisor > 0) {
divisor = -divisor;
rev = !rev;
}
int left = 1, right = Integer.MAX_VALUE, ans = 0;
while (left <= right) {
// 注意溢出,并且不能使用除法
int mid = left + ((right - left) >> 1);
boolean check = quickAdd(divisor, mid, dividend);
if (check) {
ans = mid;
// 注意溢出
if (mid == Integer.MAX_VALUE) {
break;
}
left = mid + 1;
} else {
right = mid - 1;
}
}
return rev ? -ans : ans;
}
// 快速乘
public boolean quickAdd(int y, int z, int x) {
// x 和 y 是负数,z 是正数
// 需要判断 z * y >= x 是否成立
int result = 0, add = y;
while (z != 0) {
if ((z & 1) != 0) {
// 需要保证 result + add >= x
if (result < x - add) {
return false;
}
result += add;
}
if (z != 1) {
// 需要保证 add + add >= x
if (add < x - add) {
return false;
}
add += add;
}
// 不能使用除法
z >>= 1;
}
return true;
}
}
- 位运算
class Solution {
public int divide(int dividend, int divisor) {
if(dividend == 0x80000000 && divisor == -1)
return Integer.MAX_VALUE;
if(divisor == 0)
return 0;
if(divisor == 1)
return dividend;
if(divisor == -1)
return -dividend;
long a = Math.abs((long)dividend), b = Math.abs((long)divisor);
int result = 0;
for(int i = 31; i >= 0; i--){
if((a >> i) >= b){
result += 1 << i;
a -= b << i;
}
}
return (dividend > 0 ) == (divisor > 0) ? result : - result;
}
}
- 其他方法
class Solution {
public int divide(int a, int b) {
if(a == 0x80000000 && b == -1)
return Integer.MAX_VALUE;
if(b == 0)
return 0;
boolean symbol = false;
if(a > 0){
a = -a;
symbol = !symbol;
}
if( b > 0){
b = -b;
symbol = !symbol;
}
int result = divideCore(a, b);
return symbol ? -result : result;
}
public int divideCore(int dividend, int divisor){
int result = 0;
while (divisor >= dividend){
int quotient = 1;
int value = divisor;
while(value >= 0xc0000000 && value + value >= dividend){
quotient += quotient;
value += value;
}
dividend -= value;
result += quotient;
}
return result;
}
}

浙公网安备 33010602011771号