资讯专栏INFORMATION COLUMN

leetcode-29. Divide Two Integers

darkbaby123 / 1145人阅读

摘要:题目解析用加减法实现除法用减法,每次累加被减部分,累加商,以一个固定的倍数递增坑注意循环的跳出便捷,的情况要注意。应用累加思想,可以用在提速上,效率提高如果,则是负的,则是正的

题目解析:
用加减法实现除法
用减法,每次累加被减部分,累加商, 以一个固定的倍数递增
坑: 注意 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)


文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/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元查看
<