[Leetcode] Palindrome number (no extra space is used)

Source: Internet
Author: User

It is easy to judge a palindrome string, just reverse the string and compare it to the original string. This topic clearly indicates that additional space cannot be used, so it is not feasible to use the method of splitting it into strings. Had to adopt the mathematical way: each time to take the highest and lowest phase comparison, the total number of bits can be processed out with a while, loop until the remainder and the divisor equal.

See the code for details:

Class Solution {public:    bool Ispalindrome (int x) {        if (x<0)       //special due             return false;        if (x<10)            return true;        int curmod=0;        int test=x;        while (test)        {             curmod++;             test/=10;        }        curmod--;//bit num         int left=pow (10,curmod*1.0), right=10;        while (Right<=left)        {                if (X%right!=x/left)                        return false;                x=x%left,x/=10;                left/=100;        }        return true;    }};


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[Leetcode] Palindrome number (no extra space is used)

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.