Leetcode Golang 7. Reverse Integer.go

2019-04-12 11:13:43 浏览数 (1)

版权声明:原创勿转 https://cloud.tencent.com/developer/article/1412887

思路

循环取余数

需要注意越界

code

代码语言:javascript复制
func reverse(x int) int {

	res := 0
	for x != 0 {
		carry := x % 10
		res = res*10   carry
		if res > math.MaxInt32 || res < math.MinInt32 {
			return 0
		}
		x /= 10
	}
	return res
}

0 人点赞