亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專(zhuān)欄INFORMATION COLUMN

[LeetCode] 29. Divide Two Integers

fai1017 / 2086人閱讀

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)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

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

相關(guān)文章

  • 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
  • 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
  • LeetCode刷題——29. Divide Two Integers(Part 2靠大家)

    摘要:上篇文章寫(xiě)了以我自己的思路來(lái)解決這個(gè)問(wèn)題,但是運(yùn)行時(shí)間過(guò)長(zhǎng),看了上的高效寫(xiě)法是使用位運(yùn)算的解法,當(dāng)初我自己寫(xiě)這個(gè)問(wèn)題是也想到了可以用位運(yùn)算快一點(diǎn),但是因?yàn)榛A(chǔ)差,對(duì)位運(yùn)算的掌握不牢靠,還是選擇使用了減法的思路,在此將上高效解法做一個(gè)思路分析 上篇文章寫(xiě)了以我自己的思路來(lái)解決這個(gè)問(wèn)題,但是運(yùn)行時(shí)間過(guò)長(zhǎng),看了leetcode 上的高效寫(xiě)法是使用位運(yùn)算的解法,當(dāng)初我自己寫(xiě)這個(gè)問(wèn)題是也想到了可...

    JouyPub 評(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

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

0條評(píng)論

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