-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path29.py
More file actions
35 lines (33 loc) · 973 Bytes
/
29.py
File metadata and controls
35 lines (33 loc) · 973 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2020/4/16 16:04
# @Author : Fhh
# @File : 29.py
# Good good study,day day up!
# def divide(self, dividend, divisor):
# i, a, b = 0, abs(dividend), abs(divisor)
# if a == 0 or a < b:
# return 0
#
# while b <= a:
# b = b << 1
# i = i + 1
# else:
# res = (1 << (i - 1)) + self.divide(a - (b >> 1), abs(divisor))
# if (dividend ^ divisor) < 0:
# res = -res
# return min(res, (1 << 31) - 1)
class Solution:
def divide(self, dividend, divisor):
i, a, b = 0, abs(dividend), abs(divisor)
if a == 0 or a < b:
return 0
while b <= a:
b = b << 1
i = i + 1
res = (1 << (i - 1)) + self.divide(a - (b >> 1), abs(divisor))
if (dividend ^ divisor) < 0:
res = -res
return min(res, (1 << 31) - 1)
s = Solution()
print(s.divide(23, 4))