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

Source: Internet
Author: User

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

It is easy to judge the return string. You only need to reverse the string and compare it with the original string. This question clearly indicates that no extra space is available, so it is not feasible to break it into strings. We had to use the mathematical method: each time we take the highest bit and the second bit, we can use a while to process the total number of digits first, until the remainder and the divisor are equal.

For details, see the code:

 

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 ;}};


 

 

Related Article

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.