Reverse Integer
Last updated
Last updated
class Solution {
func reverse(_ x: Int) -> Int {
var y = x
var result = 0
// 注意while條件
while (y > 0) {
// 第一次取出來的餘數,會因為while loop乘上最多次的10,變成第一個數字
result = 10 * result + y % 10
y /= 10
}
// 32bit處理
if result > Int.max || result < Int.min {
return 0
}
return result
}
}