要求:
给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。
1 class Solution { 2 func reverse(_ x: Int) -> Int { 3 var rev = 0 4 var temp = x 5 while temp != 0 { 6 let pop = temp % 10 7 temp = temp / 10 8 rev = rev * 10 + pop 9 if rev > Int32.max || (rev == (Int32.max / 10) && pop > 7) { 10 return 0; 11 } 12 if rev < Int32.min || (rev == (Int32.min / 10) && pop < -8) { 13 return 0; 14 } 15 } 16 return rev 17 } 18 }