Dashboard Temp Share Shortlinks Frames API

HTMLify

LeetCode - Reverse Integer - Go
Views: 376 | Author: abh
 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
34
35
36
37
38
39
40
41
42
func int_to_str(n int) string {
    s := ""
    if n < 0 {
        s += "-"
        n *= -1
    }
    for ;n!=0; {
        m := n % 10
        n /= 10
        d := rune(48+m)
        s += string(d)
    }
    if len(s) == 0 {
        s = "0"
    }
    return s
}
func str_to_int(s string) int {
    nag := false
    n := 0
    if s[0] == '-' {
        nag = true
    }
    for i, v := range s {
        if nag && i == 0 {
            continue
        }
        d := int(v) - 48
        n = n * 10 + d
    }
    if nag {
        n *= -1
    }
    return n
}
func reverse(x int) int {
    ans := str_to_int(int_to_str(x))
    if ans < -2147483648 || ans > 2147483648 - 1 {
        return 0
    }
    return ans
}