golang_算法: leetcode_字符串02-整数反转

版权声明:本文为博主原创文章,转载请附上博文链接! https://blog.csdn.net/weixin_43851310/article/details/88178649
//设计算法:
//弹出和推入
func reverse(x int) int {
	res := 0
	sign := 1

	if x < 0 {
		x = -x
		sign = -1
	}

	for x > 0 {
		res = res*10 + x%10
		x /= 10
	}

	res = res * sign

	if  res < math.MinInt32 || res > math.MaxInt32 {
		return 0
	}
	fmt.Println(res)
	return  res
}

func main() {
	var x int = 2921111183
	fmt.Println(reverse(x))
}

Output:

0

猜你喜欢

转载自blog.csdn.net/weixin_43851310/article/details/88178649