-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7.整数反转.cpp
49 lines (41 loc) · 968 Bytes
/
7.整数反转.cpp
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
/*
* @lc app=leetcode.cn id=7 lang=cpp
*
* [7] 整数反转
*/
// @lc code=start
#include <cmath>
class Solution
{
public:
int reverse(int x)
{
if (x == 0)
return 0;
bool isNeg = x < 0;
// check whether x is INT_MIN
if (x == -2147483648)
return 0;
if (isNeg)
x = -x;
// figure out the figures
int digitNum = log10(x) + 1;
int *digits = new int[digitNum];
for (int i = 0; i < digitNum; i++)
{
digits[i] = x % 10;
x = x / 10;
}
// reverse the order
long res = 0;
for (int i = 0; i < digitNum; i++)
res += digits[i] * pow(10, digitNum - i - 1);
// return
if ((int)res == res)
return (isNeg ? -res : res);
else
return 0;
delete[] digits;
}
};
// @lc code=end