Leetcode (9) palindrome number

Source: Internet
Author: User

Topic:

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

Some hints:

Could negative integers be palindromes? (ie,-1)

If you is thinking of converting the integer to string, note the restriction of using extra space.

You could also try reversing an integer. However, if you had solved the problem "Reverse integer", you know that the reversed integer might overflow. How do would handle such case?

There is a more generic the solving this problem.

Analysis: The topic is to determine whether an integer data is a palindrome number, we have seen the string to determine whether a palindrome string, conditioned reflex is the use of the same idea, the integer data into a string. However, it is not allowed to take up extra space, that is to say, we cannot allocate strings to store this number. Another way of thinking, palindrome number is similar to 1, 121, 1221 such data, negative numbers are obviously not palindrome. In other words, we can determine the same from the highest and lowest digits in two directions in turn. AC Code:
Class Solution {public:    bool Ispalindrome (int x) {if (x < 0) return false;int size = 0, TMP = x;while (tmp! = 0) {tmp /= 10;size++;} Whileint p = x, q = x, I, J;for (I=1, j=size; i<j; i++, j--) {int a = POW (j-1); if (p/a! = q%10) {return false; }else{p%= a;q/= 10;} Else}//forreturn true;    }};


Leetcode (9) palindrome number

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.