• [LeetCode]题解(python):029-Divide Two Integers


    题目来源:

      https://leetcode.com/problems/divide-two-integers/


    题意分析:

      不用乘法,除法和mod运算来实现一个除法。如果数值超过了int类型那么返回int的最大值。


    题目思路:

      初步来说,有两个做法。

      ①模拟除法的过程,从高位开始除,不够先右挪一位。这种方法首先要将每一位的数字都先拿出来,由于最大int类型,所以输入的长度不超过12位。接下来就是模拟除法的过程。

      ②利用左移操作来实现出发过程。将一个数左移等于将一个数×2,取一个tmp = divisor,所以将除数tmp不断左移,直到其大于被除数dividend,然后得到dividend - tmp,重复这个过程。


    代码(python):

     1 class Solution(object):
     2     def divide(self, dividend, divisor):
     3         """
     4         :type dividend: int
     5         :type divisor: int
     6         :rtype: int
     7         """
     8         ispositive = True
     9         if dividend > 0 and divisor < 0:
    10             ispositive = False
    11         if dividend < 0 and divisor > 0:
    12             ispositive = False
    13         dividend = abs(dividend);divisor = abs(divisor)
    14         if dividend < divisor:
    15             return 0
    16         num = [1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000]
    17         i = 9
    18         newdividend = []
    19         while i >= 0:
    20             tmp = 0
    21             while dividend >= num[i]:
    22                 tmp += 1;dividend -= num[i]
    23             newdividend.append(tmp); i -= 1
    24         tmpm = 0; ans = 0 ;i = 0
    25         while i < 10:
    26             while tmpm < divisor:
    27                 if i > 9:
    28                     break
    29                 j = 0; t = 0
    30                 while j < 10 and tmpm != 0:
    31                     t += tmpm; j += 1
    32                 tmpm = t + newdividend[i]; i += 1
    33                 if tmpm < divisor:
    34                     j = 0; t = 0
    35                     while j < 10 and ans != 0:
    36                         t += ans; j += 1
    37                     ans = t
    38             if tmpm >= divisor:
    39                 k = 0
    40                 while tmpm >= divisor:
    41                     tmpm -= divisor; k += 1
    42                 j = 0; t = 0
    43                 while j < 10 and ans != 0:
    44                     t += ans; j += 1
    45                 ans = t + k
    46         if ispositive:
    47             if ans > 2147483647:
    48                 return 2147483647
    49             return ans
    50         if ans >= 2147483648:
    51             return -2147483648
    52         return 0 - ans
    53                 
    模拟过程
     1 class Solution(object):
     2     def divide(self, dividend, divisor):
     3         """
     4         :type dividend: int
     5         :type divisor: int
     6         :rtype: int
     7         """
     8         ispositive = True
     9         if dividend > 0 and divisor < 0:
    10             ispositive = False
    11         if dividend < 0 and divisor > 0:
    12             ispositive = False
    13         dividend = abs(dividend);divisor = abs(divisor)
    14         if dividend < divisor:
    15             return 0
    16         tmp = divisor
    17         ans = 1
    18         while dividend >= tmp:
    19             tmp <<= 1
    20             if tmp > dividend:
    21                 break
    22             ans <<= 1
    23         tmp >>= 1
    24         nans = ans + self.divide(dividend - tmp,divisor)
    25         if ispositive:
    26             if ans > 2147483647:
    27                 return 2147483647
    28             return nans
    29         if ans >= 2147483648:
    30             return -2147483648
    31         return 0 - nans
    32                 
    左移

    转载请注明出处:http://www.cnblogs.com/chruny/p/4893254.html

  • 相关阅读:
    Java集合框架:Collections工具类
    百度编辑器多图上传返回图片绝对路径问题
    iOS开发中“此证书的签发者无效”的解决方式
    codeblocks如何watch指针
    codeblocks如何watch数组
    printf不支持%lf
    doxygen可以生成C/C++代码的文档(根据注释)
    codeblocks中右键源文件没有Rename选项?
    codeblocks中给GCC编译器加参数
    codeblocks设置当前行高亮
  • 原文地址:https://www.cnblogs.com/chruny/p/4893254.html
Copyright © 2020-2023  润新知