-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDivideTwoInteger_v0.py
More file actions
43 lines (33 loc) · 938 Bytes
/
DivideTwoInteger_v0.py
File metadata and controls
43 lines (33 loc) · 938 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
36
37
38
39
40
41
42
43
#!/usr/bin/env python
# encoding: utf-8
class Solution:
# @param {integer} dividend
# @param {integer} divisor
# @return {integer}
def divide(self, dividend, divisor):
MAX_INT = 2**31 - 1
MIN_INT = - MAX_INT - 1
if not divisor:
return MAX_INT
negative = (dividend > 0 and divisor < 0) or (dividend < 0 and divisor > 0)
dividend = abs(dividend)
divisor = abs(divisor)
a = b = 0
for n in reversed(list(popbits(dividend))):
a = (a << 1) + n
b = b << 1
if a >= divisor:
b += 1
a -= divisor
r = -b if negative else b
return min(max(MIN_INT, r), MAX_INT)
def popbits(n):
while n:
t = n >> 1
yield int(n != (t + t))
n = t
raise StopIteration()
if __name__ == '__main__':
s = Solution()
r = s.divide(-13, 5)
print r