9. Palindrome Number

来源:互联网 发布:中转国际机票 知乎 编辑:程序博客网 时间:2024/06/07 02:25

Determine whether an integer is a palindrome. Do this without extra space.

判断一个数是否为回文数。

举例说明:1234321

取出最左边的数1,最右边的数1,比较相同,首先算得输入数的位数为7位,然后1234321/1000000=1取得1,然后再用1234321%10=1;

再将1234321变为(1234321-1000000)/10=23432;循环判断。

class Solution {
public:
    bool isPalindrome(int x) 
    {

         if(x<0)

         return false;

        if(x>=0&&x<10)

        return true;

        int count(1);

        while(x/count>=10)

              {

                count*=10;


              }

        while(x>0)

                 {

                           int left=x/count;

                           int right=x%10;

                          if(left!=right)

                          return false;

                         else

                         x=x-left*count;

                         x=x/10;

                        count=count/100;

                 }

            return true;

    }

};


原创粉丝点击