Skip to content

Commit 28f8881

Browse files
authored
Create 0007.Reverse Integer.swift
1 parent f8c2457 commit 28f8881

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

0007.Reverse Integer.swift

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
func reverse(_ x: Int) -> Int {
3+
if x < 10 && x > -10 {
4+
return x
5+
}
6+
7+
var res = 0
8+
var left = x
9+
10+
while left != 0 {
11+
let remainder = left % 10
12+
left /= 10
13+
14+
res = res * 10 + remainder
15+
}
16+
17+
return res > Int32.max || res < Int32.min ? 0 : res
18+
}
19+
}

0 commit comments

Comments
 (0)