forked from lennylxx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
7.c
47 lines (40 loc) · 1023 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
#include <stdio.h>
#include <stdint.h>
// detect overflow
int check(int a, int *p) {
int64_t ret = (int64_t)a;
*p = (int)ret;
return ret >= INT32_MAX || ret <= INT32_MIN;
}
int check_add(int a, int b, int *p) {
int64_t ret = (int64_t)a + (int64_t)b;
*p = (int)ret;
return ret >= INT32_MAX || ret <= INT32_MIN;
}
int check_mul(int a, int b, int *p) {
int64_t ret = (int64_t)a * (int64_t)b;
*p = (int)ret;
return ret >= INT32_MAX || ret <= INT32_MIN;
}
int reverse(int x) {
int ret = 0;
int sign = 0;
if (x < 0) {
if (check(-x, &x)) return 0;
sign = 1;
}
while (x >= 10) {
if (check_add(ret, x % 10, &ret)) return 0;
x /= 10;
if (check_mul(ret, 10, &ret)) return 0;
}
if (check_add(ret, x, &ret)) return 0;
if (sign)
if (check(-ret, &ret)) return 0;
return ret;
}
int main() {
int x = -2147483648;
printf("%d\n", reverse(x));
return 0;
}