Skip to content

Commit 9c0ddc4

Browse files
authored
Update Q1, Q7 answer (#76)
* Update 001_Two_Sum.py * Update 007_Reverse_Integer.py Contributed by @LONGNEW
1 parent 7951e9e commit 9c0ddc4

File tree

2 files changed

+15
-42
lines changed

2 files changed

+15
-42
lines changed

python/001_Two_Sum.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,4 @@ def twoSum(self, nums, target):
5858
begin += 1
5959
else:
6060
end -= 1
61-
62-
63-
if __name__ == '__main__':
64-
# begin
65-
s = Solution()
66-
print s.twoSum([3, 2, 4], 6)
61+

python/007_Reverse_Integer.py

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,17 @@
11
class Solution:
2-
# @return an integer
2+
def reverse(self, x):
3+
# https://leetcode.com/problems/reverse-integer/
4+
flag = True if x < 0 else False
5+
if flag:
6+
x = -x
7+
x = str(x)[::-1]
38

4-
# def reverse(self, x):
5-
# max_int = 2147483647
6-
# if x == 0:
7-
# return 0
8-
# isPos = True
9-
# if x < 0:
10-
# x *= (-1)
11-
# isPos = False
12-
# ltemp = []
13-
# while x != 0:
14-
# temp = x % 10
15-
# ltemp.append(temp)
16-
# x /= 10
17-
# result = 0
18-
# # the main solution
19-
# for t in ltemp:
20-
# result = result * 10 + t
21-
# if result > max_int:
22-
# result = 0
23-
# if isPos:
24-
# return result
25-
# else:
26-
# return -1 * result
9+
if flag:
10+
x = "-" + x
2711

28-
def reverse(self, x):
29-
# Note that in Python -1 / 10 = -1
30-
res, isPos = 0, 1
31-
if x < 0:
32-
isPos = -1
33-
x = -1 * x
34-
while x != 0:
35-
res = res * 10 + x % 10
36-
if res > 2147483647:
37-
return 0
38-
x /= 10
39-
return res * isPos
12+
value = 2 ** 31
13+
x = int(x)
14+
if -value <= x < value:
15+
return x
16+
return 0
17+

0 commit comments

Comments
 (0)