码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-10-20 00:00:17      阅读:511      评论:0      收藏:0      [点我收藏+]

标签:

题目来源:

  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

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

标签:

原文地址:http://www.cnblogs.com/chruny/p/4893254.html

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