Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions solution/0029.Divide Two Integers/Solution.py
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions solution/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down