【LeetCode】29. Divide Two Integers - Java实现

1. 题目描述:

Given two integers dividend and divisor, divide two integers without using multiplication, division and mod operator.

Return the quotient after dividing dividend by divisor.

The integer division should truncate toward zero.

Example 1:

Input: dividend = 10, divisor = 3
Output: 3

Example 2:

Input: dividend = 7, divisor = -3
Output: -2

Note:

  • Both dividend and divisor will be 32-bit signed integers.
  • The divisor will never be 0.
  • Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−2^31, 2^31 − 1]. For the purpose of this problem, assume that your function returns 2^31 − 1 when the division result overflows.

2. 思路分析:

题目的意思是在不使用乘法,除法和取模操作的情况下实现整数的除法运算,整型溢出则返回最大整型。

既然不能用乘、除、取模,只能用移位操作来完成了。将除数 divisor 每次左移一位,找到最接近被除数且不大于被除数 dividend的一个数,然后将被除数减去这个数,继续之前的操作。例如:31除以3,将3不断左移,直到左移3位后达到最接近31且不大于31的数,即3*2*2*2=24(此时相当于将3乘以了8,这个8是结果的一部分),然后将31-24=7,继续之前的操作,左移3,直到最接近7且不大于7的一个数,即将3左移一位得到3*2=6(这个2也是结果的一部分),然后7-6=1,继续之前的操作,因为3已经大于1了,所以不需要左移了,即得到最后的结果为:8+2=10

3. Java代码:

源代码见我GiHub主页

代码:

public static int divide(int dividend, int divisor) {
    if (divisor == 0) {
        return Integer.MAX_VALUE;
    }

    // 用long型来存储,防止越界
    long dividendLong = dividend;
    long divisorLong = divisor;

    long dividendAbs = Math.abs(dividendLong);
    long divisorAbs = Math.abs(divisorLong);
    if (dividendAbs < divisorAbs) {
        return 0;
    }

    long tempResult = 1;
    long m = dividendAbs;
    long n = divisorAbs;
    // 通过移位找到最大的乘数,使得和 n 的乘积小于等于 m
    while ((m >> 1) >= n) {
        tempResult = tempResult << 1;
        n = n << 1;
    }

    // 递归求解
    long result = tempResult + divide((int)(m - n), (int)divisorAbs);

    // 确定最后结果的符号,及是否越界
    if ((dividend > 0 && divisor < 0) || (dividend < 0 && divisor > 0)) {
        return -result < Integer.MIN_VALUE ? Integer.MIN_VALUE : (int)-result;
    } else {
        return result > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int)result;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值