[Leetcode]Palindrome Number

来源:互联网 发布:iphone相簿导入mac 编辑:程序博客网 时间:2024/06/18 08:55

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

class Solution {public:    /*algorithm     for number abcd , compare head and tail digit meantime     if it is same, remove head and tail digit unitl digit is one or empty     this can avoid overflow     another way is we can transform the x to string and check whehter the string is palindrome    */    bool isPalindrome(int x) {        if(x < 0)return false;        int div = 1;        while(x/10 >= div)            div *= 10;        while(x){            if((x/div) != (x%10))return false;            x = x%div/10;            div /= 100;        }        return true;    }};


0 0
原创粉丝点击