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

leetcode 29-> Divide Two Integers without using multiplication, division and mod operator

时间:2019-03-14 17:58:34      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:span   ide   color   lse   turn   controls   VID   leetcode   ret   

 

class Solution(object):
    def divide(self, dividend, divisor):
        """
        :type dividend: int
        :type divisor: int
        :rtype: int
        """
        ispositive = True
        if dividend > 0 and divisor < 0:
            ispositive = False
        if dividend < 0 and divisor > 0:
            ispositive = False
        dividend = abs(dividend);divisor = abs(divisor)
        if dividend < divisor:
            return 0
        num = [1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000]
        i = 9
        newdividend = []
        while i >= 0:
            tmp = 0
            while dividend >= num[i]:
                tmp += 1;dividend -= num[i]
            newdividend.append(tmp); i -= 1
        tmpm = 0; ans = 0 ;i = 0
        while i < 10:
            while tmpm < divisor:
                if i > 9:
                    break
                j = 0; t = 0
                while j < 10 and tmpm != 0:
                    t += tmpm; j += 1
                tmpm = t + newdividend[i]; i += 1
                if tmpm < divisor:
                    j = 0; t = 0
                    while j < 10 and ans != 0:
                        t += ans; j += 1
                    ans = t
            if tmpm >= divisor:
                k = 0
                while tmpm >= divisor:
                    tmpm -= divisor; k += 1
                j = 0; t = 0
                while j < 10 and ans != 0:
                    t += ans; j += 1
                ans = t + k
        if ispositive:
            if ans > 2147483647:
                return 2147483647
            return ans
        if ans >= 2147483648:
            return -2147483648
        return 0 - ans

 

leetcode 29-> Divide Two Integers without using multiplication, division and mod operator

标签:span   ide   color   lse   turn   controls   VID   leetcode   ret   

原文地址:https://www.cnblogs.com/sea-stream/p/10531906.html

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