blob: 7bf96b84519809df404e276fd171905aa5b6a590 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
package main
import "fmt"
func reverse(x int) int {
var res int
var negative bool
if x < 0 {
negative = true
x = x * -1
}
for x > 0 {
digit := x - ((x / 10) * 10)
res = res*10 + digit
if res > (0x1 << 31) {
return 0
}
x = x / 10
}
if negative {
return res * -1
}
return res
}
func main() {
fmt.Println(reverse(123))
fmt.Println(reverse(-123))
fmt.Println(reverse(120))
fmt.Println(reverse(1534236469))
}
|