两数相除——不容许使用高级运算

两数相除——不容许使用高级运算

Divide Two Integers

  • 实现两整数相除,不容许使用乘法、除法、和取余运算。git

  • 若是结果溢出(int范围为-2147483648 ~ 2147483647),返回MAX_INTgithub

题目原文:ide

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

  • If it is overflow, return MAX_INT..ip

example leetcode

input: 1001, 2
output: 500

思路

  1. 利用减法,将被除数减去除数,减去的次数累计即为最后结果get

  2. 为了解决效率问题,能够减去除数的倍数,利用位运算,每次除数左移一位(2倍),次数相应加对应的倍数。input

  3. 若是左移一位的除数过大,除数还原。it

  4. 注意处理除法运算中正负号的问题。io

代码

class Solution(object):
    def divide(self, dividend, divisor):
        """
        :type dividend: int
        :type divisor: int
        :rtype: int
        """
        pos = (dividend < 0) is (divisor < 0)
        dividend, divisor = abs(dividend), abs(divisor)
        result = 0
        while dividend >= divisor:
            tmp, i = divisor, 1
            while dividend - tmp >= 0:
                dividend -= tmp
                result += i
                i <<= 1
                tmp <<= 1
        if not pos:
            result = -result
        return min(2147483647, max(result, -2147483648))

本题以及其它leetcode题目代码github地址: github地址

相关文章
相关标签/搜索