标签:style class blog code http tar
Determine whether an integer is a palindrome. Do this without extra space.
思路:若使用【Leetcode】Reverse Integer 的方法,判断反转后的整数是否与原整数相同,则可能出现溢出情况;又因为题目要求不适用额外空间,可以之间对比整数第一个与最后一个数的值,再依次类推。
class Solution {
public:
bool isPalindrome(int x) {
if(x < 0) return false;
int bitNum = 0;
int temp = x;
while(temp != 0)
{
temp /= 10;
bitNum++;
}
for(int i = 1; i <= bitNum / 2; i++)
{
if((x / (int)pow(10, bitNum - i)) % 10 == (x % (int)pow(10, i)) / (int)pow(10, i - 1))
continue;
else
return false;
}
return true;
}
};【Leetcode】Palindrome Number,布布扣,bubuko.com
标签:style class blog code http tar
原文地址:http://blog.csdn.net/lipantechblog/article/details/32708223