HTMLify

LeetCode - Reverse Integer - Go
Views: 19 | Author: abh
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
}

Comments

abh 2025-01-22 07:06

I should put some newlines between function definitions