-
Notifications
You must be signed in to change notification settings - Fork 5
/
solution.cpp
43 lines (37 loc) · 863 Bytes
/
solution.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
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
bool isPalindrome(int x) {
if (x < 0) return false;
int num = x, cur = 0;
while (x) {
if (cur > (INT_MAX - x % 10) / 10) return false;
cur = cur * 10 + x % 10;
x /= 10;
}
return cur == num;
}
};
struct Example {
int x;
bool ans;
};
int main() {
vector<Example> examples = {
{121, true},
{-121, false},
{10, false},
};
Solution *solve = new Solution();
for (int i = 0; i < examples.size(); i++) {
bool ans = solve->isPalindrome(examples[i].x);
if (ans == examples[i].ans) {
cout << "PASS: CASE " << i << endl;
} else {
cout << "FAIL: CASE " << i << endl;
}
}
return 0;
}