-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path9.回文数.cpp
49 lines (45 loc) · 899 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
44
45
46
47
48
49
/*
思路: 双指针+数组
* @lc app=leetcode.cn id=9 lang=cpp
*
* [9] 回文数
*/
#include <iostream>
#include <vector>
using namespace std;
// @lc code=start
class Solution {
public:
bool isPalindrome(int x) {
if (x == 0)
return true;
if (x < 0)
return false;
return reverse(x);
// cout << re << endl;
// return re == x;
}
bool reverse(int x) {
vector<int> re;
// int out = 0;
while (x > 0) {
re.push_back(x % 10);
x = x / 10;
}
for (vector<int>::iterator start = re.begin(), end = re.end() - 1;
start < end; ++start, --end) {
// cout << *start << "," << *end << endl;
if (*start != *end)
return false;
}
return true;
// out = out * 10 + (*it);
// return out;
}
};
// @lc code=end
int main() {
Solution s;
bool out = s.isPalindrome(121);
cout << out << endl;
}