diff --git a/solution/0029.Divide Two Integers/Solution.py b/solution/0029.Divide Two Integers/Solution.py new file mode 100644 index 0000000000000..1f130d1f027e1 --- /dev/null +++ b/solution/0029.Divide Two Integers/Solution.py @@ -0,0 +1,29 @@ +class Solution(object): + def divide(self, dividend, divisor): + """ + :type dividend: int + :type divisor: int + :rtype: int + """ + if dividend == 0: + return 0 + if divisor == 0: + return 2 ** 31 - 1 + + sign = (dividend < 0) ^ (divisor < 0) + + quotient = 0 + dividend = abs(dividend) + divisor = abs(divisor) + while dividend >= divisor: + tmp, i = divisor, 1 + while dividend >= tmp: + dividend -= tmp + quotient += i + tmp <<= 1 + print('Value of temp: '+str(tmp)) + i <<= 1 + print('Value of i: '+str(i)) + quotient *= (-1) ** sign + + return min(max(quotient, - 2 ** 31), 2 ** 31 - 1) diff --git a/solution/README.md b/solution/README.md index ce394c2dbd0ae..f55baa55cd1fa 100644 --- a/solution/README.md +++ b/solution/README.md @@ -164,6 +164,7 @@ ├── 0029.Divide Two Integers │   ├── README.md │   └── Solution.java +│   └── Solution.py ├── 0030.Substring with Concatenation of All Words │   ├── README.md │   └── Solution.java