Skip to content

Commit 1858ef8

Browse files
committed
LeetCode - 7. Reverse Integer
1 parent f00fbc6 commit 1858ef8

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

lowest-common-ancestor-of-a-binary-tree.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,9 @@ def lowestCommonAncestor(root: TreeNode, descendantOne: TreeNode, descendantTwo:
7474

7575

7676
if __name__ == '__main__':
77-
# tree_array = [3, 5, 1, 6, 2, 0, 8, None, None, 7, 4]
78-
# p = 5
79-
# q = 4
80-
tree_array = [1, 2]
81-
p = 1
82-
q = 2
77+
tree_array = [3, 5, 1, 6, 2, 0, 8, None, None, 7, 4]
78+
p = 5
79+
q = 4
8380
root_node, descendantNode = create_binary_tree(tree_array, p, q)
8481
lca = lowestCommonAncestor(root_node, descendantNode['p'], descendantNode['q'])
8582
print('LCA - ', lca.val)

reverse-integer.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/python3
2+
# https://leetcode.com/problems/reverse-integer/
3+
4+
5+
def reverse(x: int) -> int:
6+
result = 0
7+
remainder = abs(x)
8+
while remainder != 0:
9+
result *= 10
10+
result += remainder % 10
11+
remainder //= 10
12+
if result > 2 ** 31:
13+
return 0
14+
return result if x >= 0 else -result
15+
16+
17+
if __name__ == '__main__':
18+
# print('123 | ', reverse(123))
19+
# print('-123 | ', reverse(-123))
20+
# print('120 | ', reverse(120))
21+
# print('0 | ', reverse(0))
22+
print('1534236469 | ', reverse(1534236469), 9646324351)

0 commit comments

Comments
 (0)