Leetcode-palindrome number number is a palindrome

Source: Internet
Author: User

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


Detect whether the current number is a palindrome number, while not adding extra memory space, here is a note that negative numbers are not likely to be palindrome numbers

And then it detects each digit to compare it.

The code is still very tedious to write, the main point is that the number of digits is the number of the base and even the number of times the process of processing is different

Class Solution {Public:int geth (int x,int num)//Gets the corresponding position of the number {while (num) {x = x/10;num--;} return x%10;} BOOL Ispalindrome (int x) {    if (x < 0)    {        return false;    } int hnum = 0;int y = x;while (y) {hnum++;//record number y = Y/10;} if (hnum&1)//< odd digits {int num = Hnum/2;//<dint i = 1;while (i <= num) {int h = geth (x,num-i); int L = Geth (x,num+i); if (h!= l) {return false;} i++;}} Else{int num = Hnum/2;int i = 0;while (i < num) {int h = geth (x,num+i); int L = Geth (x,num-1-i); if (h!= l) {return false;} i++;}} return true;}};


Or put out some of the better solution on the Internet:

Here we use a base to calculate the highest level directly, 100, 1000,100000 ... when you do, use division directly.

And the comparison here is from the two sides to the middle of the comparison, then this time the odd bit position is no longer important, you do not need to write branches


So: If it's a comparison between digits, it's best to compare it with the tail.

Class Solution {public:    bool Ispalindrome (int x) {        //Start Typing your C + + solution below        //do not write I NT Main () function        if (x < 0)            return false;        if (x = = 0)            return true;                    int base = 1;        while (X/base >=)            base *=;                    while (x)        {            int leftdigit = x/base;            int rightdigit = x%;            if (leftdigit! = Rightdigit)                return false;                        X-= base * LEFTDIGIT;            Base/=;            x/=;        }                return true;    }};



Leetcode-palindrome number number is a palindrome

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.