-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9.回文数.cpp
43 lines (39 loc) · 884 Bytes
/
9.回文数.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
/*
* @lc app=leetcode.cn id=9 lang=cpp
*
* [9] 回文数
*/
// @lc code=start
#include <math.h>
using namespace std;
class Solution
{
public:
bool isPalindrome(int x)
{
if (x > -1 && x < 10)
return true;
else if (x < 0)
return false;
else
{
int digNum = log10(x) + 1;
int *digArr = new int[digNum];
int y = x;
for (int i = 0; i < digNum; i++)
{
digArr[i] = y % 10;
y = y / 10;
}
int dest = digNum / 2;
for (int i = 0; i < dest; i++)
{
if (digArr[i] != digArr[digNum - 1 - i])
return false;
}
return true;
delete[] digArr;
}
}
};
// @lc code=end