forked from illuz/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAC_binary_nlogn.py
More file actions
31 lines (26 loc) · 753 Bytes
/
Copy pathAC_binary_nlogn.py
File metadata and controls
31 lines (26 loc) · 753 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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Author: illuz <iilluzen[at]gmail.com>
# File: AC_binary_nlogn.py
# Create Date: 2015-03-06 15:28:28
# Usage: AC_binary_nlogn.py
# Descripton:
class Solution:
# @return an integer
def divide(self, dividend, divisor):
sign = (dividend < 0 and divisor > 0) or (dividend > 0 and divisor < 0)
a, b = abs(dividend), abs(divisor)
ret, c = 0, 0
while a >= b:
c = b
i = 0
while a >= c:
a -= c
ret += (1<<i)
i += 1
c <<= 1
if sign:
ret = -ret
return min(max(-2147483648, ret), 2147483647)
s = Solution()
print s.divide(1, 1)