Divide Two Integers

不能使用乘法,除法和mod operator,实现除法功能。

Divide two integers without using multiplication, division and mod operator.

If it is overflow, return MAX_INT.

用减法可以实现,但是太慢了。算法里所做的优化都是为了节省时间。

不能忽视溢出的问题。例如,被除数是Integer.MIN_VALUE,除以-1,得到的结果对于Int型来说就溢出了,因此返回Integer.MAX_VALUE

先处理符号,处理两个极端情况。其余的先转为long正数后递归处理。

除数每次左移位数都增加,这加快了运算效率。把每次递归产生的左移次数都加起来,即是结果。

Java代码:

    public int divide(int dividend, int divisor) {
        int sign = ((dividend ^ divisor)>>>31) == 1? -1:1;
        if (dividend == 0) {
            return 0;
        }
        if (divisor == 0) {
            return Integer.MAX_VALUE;
        }
        if(divisor == -1 && dividend == Integer.MIN_VALUE) {
            return Integer.MAX_VALUE;//overflow
        }
        long n1 = Math.abs((long) dividend);//avoid overflow
        long n2 = Math.abs((long) divisor);
        return (int) (div(n1,n2)*sign);
    }
        public long div(long n1, long n2){
        if (n1 < n2) {
            return 0;
        }
        int i = 0;
        while(n1 > (n2 << (i+1))){
            i++;
        }
        return (1<<i) + div(n1 - (n2<<i), n2);
    }

这个问题有3点启示:

1、注意溢出的问题,该转换的就转换

2、关注运行效率的问题

3、移位方法的运用

时间: 2024-10-11 09:37:27

Divide Two Integers的相关文章

29. Divide Two Integers

Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. (1) log int divide(int dividend, int divisor) { if(dividend == 0) return 0; if(divisor == 0) return INT_MAX; double t1 = log(fabs(dividend

[LeetCode] Divide Two Integers

In this problem, we are asked to divide two integers. However, we are not allowed to use division, multiplication and mod operations. So, what else can be use? Well, bit manipulations. Let's look at an example and see how bit manipulation might help.

【leetcode】Divide Two Integers (middle)☆

Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 思路: 尼玛,各种通不过,开始用纯减法,超时了. 然后用递归,溢出了. 再然后终于开窍了,用循环,把被除数每次加倍去找答案,结果一遇到 -2147483648 就各种不行, 主要是这个数一求绝对值就溢出了. 再然后,受不了了,看答案. 发现,大家都用long long来解决溢

LeetCode --- 29. Divide Two Integers

题目链接:Divide Two Integers Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 这道题的要求是在不使用乘法.除法.取模运算的前提下实现两个整数相除.如果溢出,返回MAX_INT. 这道题的直接思路是用被除数不断减去除数,直到为0.这种方法的迭代次数是结果的大小,即比如结果为n,算法复杂度是O(n). 可以

【leetcode】Divide Two Integers

题目: Divide two integers without using multiplication, division and mod operator. 解析:不使用乘号.除号和余号实现两个整数的除法.该题可以利用移位操作数求解,主要分为三个步骤: (1)先求两个int类型整数的绝对值,注意要将int类型转化成long类型才能求绝对值:因为int类型的范围是-2147483648~2147483647,如果某个数为-2147483648,其绝对值会溢出. (2)对被除数a和除数b进行移位

每日算法之二十五:Divide Two Integers

Divide two integers without using multiplication, division and mod operator. 不使用乘法.除法和求模运算求两个数相除. class Solution { public: long long internalDivide(unsigned long long dividend,unsigned long long divisor) { if(dividend<divisor) return 0; int result =

Divide Two Integers leetcode

题目:Divide Two Integers Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 看讨论区大神的思路: In this problem, we are asked to divide two integers. However, we are not allowed to use division, multi

[Math]Divide Two Integers

otal Accepted: 54356 Total Submissions: 357733 Difficulty: Medium Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 1.除法转换为加法,加法转换为乘法,y个x相加的结果就是x*y. 假设结果为dividend的一半,用此值与divisor相乘,如果相乘结果大于

LeetCode 29 Divide Two Integers(两个整数相除)(*)

翻译 不用乘法.除法.取余操作,将两个数相除. 如果它溢出了,返回MAX_INT 原文 Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 代码 一心扑到了递归上,可惜没能写出来----烦躁至极还是找了别人的答案-- class Solution { public: int divide(int dividend, int d