Skip to content

Commit 66467ca

Browse files
authored
Merge pull request soapyigu#224 from maligh/master
Fix Reverse Integer
2 parents 423eb6b + 60e7b01 commit 66467ca

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

Math/ReverseInteger.swift

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,16 @@
77
*/
88

99
class ReverseInteger {
10-
func reverse(x: Int) -> Int {
10+
func reverse(_ x: Int) -> Int {
1111
var res = 0
1212
var x = x
13-
1413
while x != 0 {
15-
res = res * 10 + x % 10
16-
x = x / 10
17-
18-
if res > Int(Int32.max) || res < Int(Int32.min) {
14+
if res > Int(Int32.max) / 10 || res < Int(Int32.min) / 10 {
1915
return 0
2016
}
17+
res = res * 10 + x % 10
18+
x = x / 10
2119
}
22-
2320
return res
2421
}
25-
}
22+
}

0 commit comments

Comments
 (0)