-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.cpp
More file actions
56 lines (45 loc) · 1.16 KB
/
Solution.cpp
File metadata and controls
56 lines (45 loc) · 1.16 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
// Solution by Imtiaz Ahmed (Github: tiazahmd)
#include <iostream>
#include <vector>
#include "../Test.h"
using namespace std;
using namespace leetcode;
class Solution {
public:
bool isPalindrome(int x) {
if (x >= 0 && x < 10) {
return true;
} else if (x >= 10) {
vector<int> vec;
long div = 10;
int num = 0;
int temp = x;
while (temp != 0) {
num = temp % div;
temp = temp / div;
vec.push_back(num);
}
for (int i = 0; i < vec.size(); i++) {
if (i == vec.size() - 1)
return true;
else if (vec[i] != vec[vec.size() - 1 - i])
return false;
}
} else {
return false;
}
return false;
};
};
int main() {
Solution s;
test("Test case: ", s.isPalindrome(121), true);
test("Test case: ", s.isPalindrome(120), false);
test("Test case: ", s.isPalindrome(-121), false);
test("Test case: ", s.isPalindrome(11), true);
test("Test case: ", s.isPalindrome(9), true);
test("Test case: ", s.isPalindrome(0), true);
test("Test case: ", s.isPalindrome(10022001), true);
test("Test case: ", s.isPalindrome(1001), true);
return 0;
}