Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
Example 1:
Input: 121
Output: true
Example 2:
Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Example 3:
Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
本来想用char数组存储字符,然后最初最末对比,结果C语言不能开辟没有定义长度的数组。
所以最后就改成了先取反再比较,具体如下,对X的先决判断可以缩短运行时间。
bool isPalindrome(int x) {
int y=0;
int z=x;//注意否则x最终为个位数,不再是原来的数值,所以必须提前存储一下
if(x<0)
{return false;}
else{
while(z!=0)
{y=y*10+z%10;
z=z/10;}
if(x==y)
{return true;}
else
{return false;}
}
}
java版本内容不变。