-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverseInteger.cpp
More file actions
108 lines (96 loc) · 1.93 KB
/
reverseInteger.cpp
File metadata and controls
108 lines (96 loc) · 1.93 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。
示例 1:
输入: 123
输出: 321
示例 2:
输入: -123
输出: -321
示例 3:
输入: 120
输出: 21
注意:
假设我们的环境只能存储得下 32 位的有符号整数,则其数值范围为 [−231, 231 − 1]。请根据这个假设,如果反转后整数溢出那么就返回 0。
*/
#include <iostream>
#include <limits>
using namespace std;
//错误解法
int reverse(int x)
{
if (x == 0)
return x;
//正负号
bool gtZero = true;
long x1 = x;
if (x1 < 0)
{
gtZero = false;
x1 = 0 - x1;
}
//结果数组
int res[10];
int count = 0;
//分解 存入数组
while (x1 > 0)
{
int temp = x1 % 10;
x1 /= 10;
res[count++] = temp;
}
int result = 0;
//数字拼接
for (int i = 0; i < count - 1; i++)
{
if (i == 0 && res[i] == 0)
{
continue;
}
else
{
if (result > INT32_MAX / 10 || result == INT32_MAX && res[i + 1] > 7)
{
return 0;
}
result += res[i];
result *= 10;
}
}
result += res[count - 1];
if (!gtZero)
result = 0 - result;
return result;
}
//通过算法
int reverse2(int x)
{
if ((x < 10) &&( x > -10))
return x;
long result = 0;
const int int_max=0x7fffffff;
const int int_min=0x80000000;
//正负号
bool flag = true;
if (x < 0)
{
x = 0 - x;
flag = false;
}
//分解的同时拼接
while (x != 0)
{
result = result * 10 + x % 10;
x /= 10;
}
if (!flag)
result = 0 - result;
if ((result > int_max) || (result < int_min))
return 0;
return result;
}
int main()
{
int result = reverse2(-2147483648);
cout << result << endl;
return 0;
}