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

資訊專欄INFORMATION COLUMN

[LeetCode] 29. Divide Two Integers

fai1017 / 2039人閱讀

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

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

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

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

相關(guān)文章

  • leetcode-29. Divide Two Integers

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

    darkbaby123 評論0 收藏0
  • leetcode29 Divide Two Integers

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

    cnio 評論0 收藏0
  • leetcode 29 Divide Two Integers

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

    馬龍駒 評論0 收藏0
  • LeetCode刷題——29. Divide Two Integers(Part 2靠大家)

    摘要:上篇文章寫了以我自己的思路來解決這個問題,但是運行時間過長,看了上的高效寫法是使用位運算的解法,當初我自己寫這個問題是也想到了可以用位運算快一點,但是因為基礎(chǔ)差,對位運算的掌握不牢靠,還是選擇使用了減法的思路,在此將上高效解法做一個思路分析 上篇文章寫了以我自己的思路來解決這個問題,但是運行時間過長,看了leetcode 上的高效寫法是使用位運算的解法,當初我自己寫這個問題是也想到了可...

    JouyPub 評論0 收藏0
  • [Leetcode] Divide Two Integers 整數(shù)整除

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

    張春雷 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<