-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheg_hacker_palindrome.cpp
63 lines (53 loc) · 1.17 KB
/
eg_hacker_palindrome.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <string>
using namespace std;
class Solution
{
public:
bool IsPalindrome(string);
bool IsPalindrome(int);
};
bool Solution::IsPalindrome(string P)
{
//cout << P.length()/2 << endl;
for(int i = 0; i < P.length()/2; i++)
{
if (P[i] != P[P.length() - i - 1])
return false;;
}
return true;
}
/* Convert integer to string and process comparing first to last element until half the length is reached */
/*
bool Solution::IsPalindrome(int P)
{
string str = to_string(P);
return IsPalindrome(str);
}
*/
/* Leetcode suggested solution
*
*/
bool Solution::IsPalindrome(int P)
{
if(P < 0 || ((P % 10 == 0) && P!= 0))
return false;
int revertedNumber = 0;
while( P > revertedNumber)
{
revertedNumber = revertedNumber * 10 + P % 10;
P /= 10;
}
return P == revertedNumber || P == revertedNumber/10;
}
int main()
{
string S = "ABCDEFDCBA";
Solution s;
string ans;
ans = s.IsPalindrome(S) ? "Yes" : "No";
cout << ans << endl;
ans = s.IsPalindrome(123421) ? "Yes" : "No";
cout << ans << endl;
return 0;
}