0009 Palindrome Number
问题概述
解题思路
class Solution
{
public:
bool isPalindrome(int x)
{
std::string x_string1 = std::to_string(x);
std::string x_string2 = x_string1;
std::reverse(x_string1.begin(), x_string1.end());
bool flag = x_string1 == x_string2;
return flag;
}
};
class Solution
{
public:
bool isPalindrome(int x)
{
bool flag = false;
int x_t = x;
if (x < 0)
{
flag = false;
}
else
{
long long int x_reverse = 0;
while (x > 0)
{
x_reverse = x_reverse * 10 + x % 10;
x /= 10;
}
//std::cout << "x = " << x << std::endl;
//std::cout << "x_reverse = " << x_reverse << std::endl;
if (x_reverse == x_t)
{
flag = true;
}
}
return flag;
}
};
class Solution
{
public:
bool isPalindrome(int x)
{
bool flag = true;
std::string x_string = std::to_string(x);
std::stack<char> stk;
int mid = x_string.length() / 2;
for (int i = 0; i < mid; i++)
{
stk.push(x_string[i]);
}
for (int i = (x_string.length() % 2 == 0 ? mid : mid + 1); i < x_string.length(); i++)
{
if (x_string[i] != stk.top())
{
flag = false;
return flag;
}
stk.pop();
}
return flag;
}
};