9. Palindrome Number

9. Palindrome Number

My Submissions
Question
Total Accepted: 95908  Total Submissions: 319671  Difficulty: Easy

Determine 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;
        while(key)
        {
            ans*=10;
            ans+=key%10;
            key=key/10;
        }
        if(ans==x)
             return true;
        else
            return false;
    }
};


注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!

原文地址:http://blog.csdn.net/ebowtang/article/details/50365751

原作者博客:http://blog.csdn.net/ebowtang

你可能感兴趣的:(LeetCode,数据结构,算法,面试,数学)