-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path29.divide-two-integers.py
56 lines (44 loc) · 1.3 KB
/
29.divide-two-integers.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
46
47
48
49
50
51
52
53
54
55
56
# https://leetcode-cn.com/problems/divide-two-integers/
class Solution1:
'''
Date: 2022.05.05
Pass/Error/Bug: 1/2/4
执行用时: 52 ms, 在所有 Python3 提交中击败了 14.97% 的用户
内存消耗:14.9 MB, 在所有 Python3 提交中击败了 69.95% 的用户
'''
def divbysub(self, dividend: int, divisor: int) -> tuple[int, int]:
ds = 0
rs = 0
while dividend >= divisor:
dividend = dividend - divisor
ds += 1
rs = dividend
return (ds, rs)
def divide(self, dividend: int, divisor: int) -> int:
flag = 0
if dividend < 0:
flag += 1
if divisor < 0:
flag += 1
dividend = abs(dividend)
divisor = abs(divisor)
if divisor == 1:
if flag == 1:
r = 0 - dividend
else:
r = dividend
if r > 2**31-1:
return 2**31-1
else:
return r
dss = ''
rs = ''
for s_dividend in str(dividend):
(ds, rs) = self.divbysub(int(rs + s_dividend), divisor)
rs = str(rs)
ds = str(ds)
dss += ds
if flag == 1:
return 0 - int(dss)
else:
return int(dss)