码迷,mamicode.com
首页 > 其他好文 > 详细

029 Divide Two Integers

时间:2015-08-08 16:15:09      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:

029 Divide Two Integers

class Solution:
    # @param {integer} dividend
    # @param {integer} divisor
    # @return {integer}
    def divide(self, dividend, divisor):
        intMax, intMin = 2147483647, -2147483648
        if dividend==0 or divisor == 0:
            return 0
        isNeg = (dividend * divisor < 0)
        dividend, divisor = abs(dividend), abs(divisor)
        ans = 0
        tmp = 1
        d = divisor
        while dividend >= divisor:
            if dividend >= d:
                dividend -= d
                ans += tmp
                tmp += tmp
                d += d
            else:
                tmp = 1
                d = divisor
        if isNeg:
            return max(-ans, intMin)
        return min(ans, intMax)

 

029 Divide Two Integers

标签:

原文地址:http://www.cnblogs.com/dapanshe/p/4713123.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!