We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 423eb6b + 60e7b01 commit 66467caCopy full SHA for 66467ca
Math/ReverseInteger.swift
@@ -7,19 +7,16 @@
7
*/
8
9
class ReverseInteger {
10
- func reverse(x: Int) -> Int {
+ func reverse(_ x: Int) -> Int {
11
var res = 0
12
var x = x
13
-
14
while x != 0 {
15
- res = res * 10 + x % 10
16
- x = x / 10
17
18
- if res > Int(Int32.max) || res < Int(Int32.min) {
+ if res > Int(Int32.max) / 10 || res < Int(Int32.min) / 10 {
19
return 0
20
}
+ res = res * 10 + x % 10
+ x = x / 10
21
22
23
return res
24
25
-}
+}
0 commit comments