Skip to content

Commit e6b562f

Browse files
authored
Merge pull request #93 from nhimanshujain/main
Added solution to Leetcode Question - 7 | Reverse Integer
2 parents e4a64ac + 12e1d27 commit e6b562f

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

0007/README.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Reverse Integer
2+
3+
Given a signed 32-bit integer ```x```, return ```x``` with its digits reversed. If reversing ```x``` causes the value to go outside the signed 32-bit integer range ```[-231, 231 - 1]```, then return ```0```.
4+
5+
**Assume the environment does not allow you to store 64-bit integers (signed or unsigned).**
6+
7+
8+
## Examples
9+
10+
* Example 1
11+
```
12+
Input: x = 123
13+
Output: 321
14+
```
15+
16+
* Example 2
17+
```
18+
Input: x = -123
19+
Output: -321
20+
```
21+
22+
* Example 3
23+
```
24+
Input: x = 120
25+
Output: 21
26+
```
27+
28+
* Example 4
29+
```
30+
Input: x = 0
31+
Output: 0
32+
```
33+
34+
## Constraints
35+
* -231 <= x <= 231 - 1

0007/reverse_integer.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution:
2+
def reverse(self, x):
3+
s = str(x)
4+
res = (int('-' + s[1:][::-1]) if s[0] == '-' else int(s[::-1]))
5+
return (res if -2147483648 <= res <= 2147483647 else 0)

0 commit comments

Comments
 (0)