-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7.整数反转.c
58 lines (51 loc) · 1006 Bytes
/
7.整数反转.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
* @lc app=leetcode.cn id=7 lang=c
*
* [7] 整数反转
*
* https://leetcode-cn.com/problems/reverse-integer/description/
*
* algorithms
* Easy (33.39%)
* Likes: 1628
* Dislikes: 0
* Total Accepted: 261.5K
* Total Submissions: 780.1K
* Testcase Example: '123'
*
* 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。
*
* 示例 1:
*
* 输入: 123
* 输出: 321
*
*
* 示例 2:
*
* 输入: -123
* 输出: -321
*
*
* 示例 3:
*
* 输入: 120
* 输出: 21
*
*
* 注意:
*
* 假设我们的环境只能存储得下 32 位的有符号整数,则其数值范围为 [−2^31, 2^31 − 1]。请根据这个假设,如果反转后整数溢出那么就返回
* 0。
*
*/
// @lc code=start
int reverse(int x)
{
int max = 0x7fffffff;
int min = 0x80000000;
long res = 0;
for (; x; res = res * 10 + x % 10, x /= 10);
return res < min || res > max ? 0 : res;
}
// @lc code=end