LeetCode 7. 整数反转

题目描述

7. 整数反转

image-20250510070143274

思路分析

image-20250510070749251

参考代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
func reverse(x int) int {
	var res int

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

	for x != 0 {
		pop := x % 10
		x /= 10

		if res > (math.MaxInt32-pop)/10 {
			return 0
		}
		res = res*10 + pop
	}

	return sign * res
}

➡️ 点击查看 Java 题解

1
write your code here
本文作者:
本文链接: https://hgnulb.github.io/blog/2025/02923903
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处!