-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy path29_DivideTwoIntegers.py
executable file
·45 lines (37 loc) · 1.01 KB
/
29_DivideTwoIntegers.py
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
44
45
#! /usr/bin/env python
# -*- coding: utf-8 -*-
class Solution(object):
# According to:
# https://leetcode.com/discuss/38997/detailed-explained-8ms-c-solution
# Key concept:
# division simply requires us to find how many times we can subtract the
# divisor from the the dividend without making the dividend negative.
def divide(self, dividend, divisor):
if divisor == 0:
return -1
# Make sure it's positive or negative
positive = (dividend < 0) is (divisor < 0)
dividend, divisor = abs(dividend), abs(divisor)
answer = 0
while dividend >= divisor:
multiple, temp = 1, divisor
while dividend >= (temp << 1):
multiple <<= 1
temp <<= 1
dividend -= temp
answer += multiple
if not positive:
answer = -answer
if (answer > 2147483647) or (answer < -2147483648):
return 2147483647
return answer
"""
0
1
12
3
125
-4
1
-1
"""