成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專(zhuān)欄INFORMATION COLUMN

[LintCode] Divide Two Integers

NervosNetwork / 1627人閱讀

摘要:首先,分析溢出條件,設(shè)置符號(hào)位,然后取絕對(duì)值運(yùn)算。原理如下,被除數(shù),除數(shù),設(shè)等于。如,,,所以商里必定有一個(gè)的次方存入,然后。然后被除數(shù)減去,繼續(xù)。此時(shí),,循環(huán)結(jié)束。再舉一個(gè)例子看得懂的版本綜合一下

Problem

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

If it is overflow, return 2147483647.

Example

Given dividend = 100 and divisor = 9, return 11.

Note

首先,分析溢出條件,設(shè)置符號(hào)位,然后取絕對(duì)值運(yùn)算。
原理如下,被除數(shù)a,除數(shù)b,設(shè)c等于b。
在b<=a時(shí),令除數(shù)c每次乘以2,增大到小于等于被除數(shù)a的時(shí)候,c乘了幾次,商里就會(huì)有一個(gè)2的幾次方。如25 / 4,4 * 4 < 25,4 * 8 > 25,所以商里必定有一個(gè)4(2的2次方)存入temp,然后res += temp
然后被除數(shù)a減去c,繼續(xù)check。a = 25 - 4 * 4 = 9,c = 4 * 2 = 8,所以商里必定有一個(gè)8 / 4 = 2(2的1次方)存入temp。此時(shí)a = 9 - 8 = 1,a < b,循環(huán)結(jié)束。res = 4 + 2 = 6,為所求。

再舉一個(gè)例子:

13 / 3, a = 13, b = 3, c = 3:
c = 3, temp = 1; c = 6, temp = 2; c = 12; temp = 4;
c = 3, res = 4, a = 1, a < b, return res = 4.
Solution
public class Solution {
    public int divide(int dividend, int divisor) {
        long res = 0;
        boolean neg = (dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0);
        long a = Math.abs((long)dividend);
        long b= Math.abs((long)divisor);
        while (a >= b) {
            long c = b;
            long temp = 0;
            while (c <= a) {
                temp = temp == 0 ? 1 : temp << 1;
                c = c << 1;
            }
            c = c >> 1;
            a -= c;
            res += temp;
        }
        if (res >= Integer.MAX_VALUE) res = neg ? Integer.MIN_VALUE : Integer.MAX_VALUE;
        else if (neg) res = -res;
        return (int)res;
    }
}

看得懂的版本:

public class Solution {
    public int divide(int dividend, int divisor) {
        boolean isNeg = (dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0);
        if (dividend == 0) return 0;
        if (divisor == 0) return dividend > 0 ? Integer.MAX_VALUE: Integer.MIN_VALUE;
        long A = Math.abs((long)dividend); //long inside Math.abs
        long B = Math.abs((long)divisor);
        if (A < B) return 0;
        int res = 0;
        long ans = helper(A, B);
        if (ans >= Integer.MAX_VALUE) res = isNeg ? Integer.MIN_VALUE : Integer.MAX_VALUE; //distinguish corner cases
        else res = isNeg ? -(int)ans : (int)ans;
        return res;
    }
    public long helper(long A, long B) {
        if (A < B) return 0;
        long sum = B;
        long res = 1;
        while (sum+sum <= A) {
            sum += sum;
            res += res;
        }
        return res + helper(A-sum, B);
    }
}

綜合一下

public class Solution {
    public int divide(int dividend, int divisor) {
        int sign = 1;
        if ((dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0)) sign = -1;
        long A = Math.abs((long)dividend);
        long B = Math.abs((long)divisor);
        if (A < B) return 0;
        long res = 0;
        while (A >= B) {
            long sum = B;
            long temp = 1;
            while (sum+sum < A) {
                sum += sum;
                temp += temp;
            }
            A -= sum;
            res += temp;
        }
        if (res >= Integer.MAX_VALUE) res = sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
        else res = res * sign;
        return (int)res;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/65546.html

相關(guān)文章

  • [LeetCode] 29. Divide Two Integers

    Problem 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 divisi...

    fai1017 評(píng)論0 收藏0
  • leetcode-29. Divide Two Integers

    摘要:題目解析用加減法實(shí)現(xiàn)除法用減法,每次累加被減部分,累加商,以一個(gè)固定的倍數(shù)遞增坑注意循環(huán)的跳出便捷,的情況要注意。應(yīng)用累加思想,可以用在提速上,效率提高如果,則是負(fù)的,則是正的 題目解析: 用加減法實(shí)現(xiàn)除法 用減法,每次累加被減部分,累加商, 以一個(gè)固定的倍數(shù)遞增 坑: 注意 while循環(huán)的跳出便捷,= 的情況要注意。應(yīng)用:累加思想,可以用在提速上,效率提高 Given two ...

    darkbaby123 評(píng)論0 收藏0
  • [Leetcode] Divide Two Integers 整數(shù)整除

    摘要:位操作法復(fù)雜度時(shí)間空間思路我們?cè)O(shè)想,本來(lái)應(yīng)該的得到余,那么如果我們把忽略余數(shù)后分解一下,,也就是,也就是把商分解為,所以商的二進(jìn)制是。我們可以不斷的將乘的一次方,二次方,等等,直到找到最大那個(gè)次方,在這里是的四次方。 Divide Two Integers Divide two integers without using multiplication, division and m...

    張春雷 評(píng)論0 收藏0
  • leetcode29 Divide Two Integers

    摘要:題目要求在不使用乘法,除法和求余操作的情況下,計(jì)算兩個(gè)整數(shù)相除的結(jié)果。如果溢出了,則返回最大值。在這里核心思路是使用逆向二分法和遞歸的思路來(lái)進(jìn)行計(jì)算。在這里我們使用取值范圍更廣的來(lái)處理數(shù)值溢出的場(chǎng)景。 題目要求 Divide two integers without using multiplication, division and mod operator. If it is o...

    cnio 評(píng)論0 收藏0
  • leetcode 29 Divide Two Integers

    摘要:很容易想到,我們每次用被除數(shù)減去除數(shù),進(jìn)行減法的次數(shù)就是最終結(jié)果。這道題的采取了一種類(lèi)似二分查找的思想。除了這些,這道題還要注意一些邊界情況的判斷,例如除數(shù)或被除數(shù)為,值溢出等。 題目詳情 Divide two integers without using multiplication, division and mod operator.If it is overflow, retu...

    馬龍駒 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<