9. Palindrome Number
My SubmissionsTotal Accepted: 95908 Total Submissions: 319671 Difficulty: EasyDetermine whether an integer is a palindrome. Do this without extra space.
click to show spoilers.
Subscribe to see which companies asked this question
Hide Tags Math Show Similar Problems
//思路理清楚再寫代碼:既然不能申請額外的空間,那麼最好就原地判斷
//從x的低位一位一位的取出來形成新數的高位
//最後看是否與原數相等
class Solution {
public:
bool isPalindrome(int x) {
if(x<0)
return false;
int key=x;
int ans=0,carry=0;
while(key)
{
ans*=10;
carry=key%10;
ans+=carry;
key=key/10;
}
if(ans==x)
return true;
else
return false;
}
};
此方法的性能:還不錯。