-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.cpp
More file actions
78 lines (61 loc) · 1.62 KB
/
Solution.cpp
File metadata and controls
78 lines (61 loc) · 1.62 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Solution by Imtiaz Ahmed (Github: tiazahmd)
#include <iostream>
#include <vector>
#include "../Test.h"
using namespace std;
using namespace leetcode;
class Solution {
public:
string isPalindrome(int x) {
if (x >= 0 && x < 10) {
return "true";
} else if (x >= 10) {
vector<int> vec;
long div = 10;
long ct = 1;
int num = 0;
int digCount = 0;
int temp = x;
while (temp != 0) {
temp /= 10;
digCount++;
}
for (int i = 0; i < digCount; i++) {
num = x % div;
num /= ct;
div *= 10;
ct *= 10;
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 stringToInteger(string input) { return stoi(input); }
string boolToString(bool input) { return input ? "True" : "False"; }
int main() {
Solution s;
test("Test case: ", s.isPalindrome(121), "true");
test("Test case: ", s.isPalindrome(120), "true");
test("Test case: ", s.isPalindrome(-121), "true");
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");
// string line;
// while (getline(cin, line)) {
// int x = stringToInteger(line);
// bool ret = Solution().isPalindrome(x);
// string out = boolToString(ret);
// cout << out << endl;
// }
return 0;
}