topic
Given a 32-bit signed integer, you need to invert the digits on each of the integers. If the integer overflows after the inversion, it returns 0
For example
- Input: 123 Output: 321
- Input: -123 Output: -321
- Input: 120 Output: 21
Parsing
Keep taking more than 10
GO answer
func reverse(x int) int {
max := 1 << 31
min := 0 - max
rev := 0
for {
if (x == 0) {
break
}
rev = rev * 10 + x % 10
if (rev > max || rev < min) {
return 0
}
x = x / 10
}
return rev
}