国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

leetcode-29. Divide Two Integers

darkbaby123 / 1146人閱讀

摘要:題目解析用加減法實現除法用減法,每次累加被減部分,累加商,以一個固定的倍數遞增坑注意循環的跳出便捷,的情況要注意。應用累加思想,可以用在提速上,效率提高如果,則是負的,則是正的

題目解析:
用加減法實現除法
用減法,每次累加被減部分,累加商, 以一個固定的倍數遞增
坑: 注意 while循環的跳出便捷,= 的情況要注意。
應用:累加思想,可以用在提速上,效率提高
"""

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: [?231,  231 ? 1]. For the purpose of this problem, assume that your function returns 231 ? 1 when the division result overflows.

"""
import time
import math
class Solution:
    def divide(self, dividend, divisor):
        """
        :type dividend: int
        :type divisor: int
        :rtype: int
        """
        sign=(dividend>0)^(divisor>0)  #如果 ==1,則是 負的 ==0,則是正的
        dividend_current=int(math.fabs(dividend))
        divisor_current=int(math.fabs(divisor))
        quotient=0
        quotient_base=1

        while dividend_current>0:
            print(dividend_current,divisor_current,quotient)
            while dividend_current>=divisor_current:
                quotient+=quotient_base
                dividend_current-=divisor_current
                divisor_current*=2   #
                quotient_base*=2
                # time.sleep(1)
            while divisor<=dividend_current= 2 ** 31:
            return 2 ** 31 - 1
        elif ans <= -2 ** 31 - 1:
            return -2 ** 31
        else:
            return ans
if __name__=="__main__":
    st=Solution()
    dividend=128
    dividend=-2147483648
    divisor=1
    # out=st.divide(-128,3)
    out=st.divide(dividend,divisor)
    print(out)


文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/42153.html

相關文章

  • [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 評論0 收藏0
  • leetcode29 Divide Two Integers

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

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

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

    馬龍駒 評論0 收藏0
  • [Leetcode] Divide Two Integers 整數整除

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

    張春雷 評論0 收藏0
  • [LintCode] Divide Two Integers

    摘要:首先,分析溢出條件,設置符號位,然后取絕對值運算。原理如下,被除數,除數,設等于。如,,,所以商里必定有一個的次方存入,然后。然后被除數減去,繼續。此時,,循環結束。再舉一個例子看得懂的版本綜合一下 Problem Divide two integers without using multiplication, division and mod operator. If it is ...

    NervosNetwork 評論0 收藏0

發表評論

0條評論

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