Leetcode Golang 7. Reverse Integer.go

思路

循环取余数
需要注意越界

code

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
}

猜你喜欢

转载自blog.csdn.net/anakinsun/article/details/88867281
今日推荐