class Solution {
public:
bool isPalindrome(int x) {
if(x%10=x)
{
return true;
}
vector edd;
while(!res=0)
{
int res=x%10;
int x/=10;
edd.push_back(res);
}
int n=edd.size();
for(i=0;i<n;i++)
{
for(i=n-1;i>9;i--)
{
if(edd[i]==edd[j])
{
return true;
}
else
{
return false;
}
}
}
}
};
问:这是我写的程序,我写的有问题,希望来个负责任的大佬给我更改错误,我的思路是将整数x放入vector中,比较vector中第一个数和最后一个数,第二个数和倒数第二个数是否相等等等。希望来个负责任的大佬,在我的思路为基础完整地更改一下我的错误。最后一个问题,int定义的整数,可以用size来判断int长度大小吗
class Solution
{
public:
bool isPalindrome(int x)
{
int n = static_cast<int>(std::log10(x) + 1);
int reverted = 0;
for (int i = 0; i < n / 2; i++)
{
reverted = reverted * 10 + x % 10;
x /= 10;
}
if (n % 2 != 0)
x /= 10;
return x == reverted;
}
};
class Solution {
public:
bool isPalindrome(int x) {
if(x%10=x)
{
return true;
}
vector edd;
while(x != 0)
{
int res=x%10;
edd.push_back(x/10);
x = res;
}
int n=edd.size();
for(int i=0, j=n-1;i<j;i++,j--)
{
if(edd[i]!=edd[j])
{
return false;
}
}
return true;
}
class Solution
{
public:
bool isPalindrome(int x)
{
if(x < 0) return false;
if (x % 10 == x)
{
return true;
}
vector<int> edd;
while (x != 0)
{
int res = x / 10;
edd.push_back(x % 10);
x = res;
}
int n = edd.size();
for (int i = 0, j = n - 1; i < j; i++, j--)
{
if (edd[i] != edd[j])
{
return false;
}
}
return true;
}
};
class Solution {
public:
bool isPalindrome(int x) {
string s = to_string(x);
int i = 0,j = s.size() - 1;
while(i<j)
{
if(s[i] != s[j]) return false;
i++;
j--;
}
return true;
}
};
class Solution {
public:
bool isPalindrome(int x) {
string s = to_string(x),s1;
reverse(s.begin(),s.end());
return s==s1;
}
};