Leetcode (+) palindrome number (palindrome digit)

Source: Internet
Author: User

Title Description

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

Some hints:
Could negative integers be palindromes? (ie,-1)
- negative number is not a palindrome

If you is thinking of converting the integer to string, note the restriction of using extra space.
-- converting a number to a string, we have made a decision whether the string is a palindrome, but this will use extra space does not meet the requirements of the topic.

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?
- reversing the numbers, judging whether they are the same before and after reversal, but there is a overflow problem (no "Reverse Integer") and it is not clear why it overflows. )

The subject asks whether the number is a palindrome and requires that no additional memory space be used. The subject can be judged with two pointers using a string-like palindrome. Of course, for a number, we can't move two pointers from front to back, pointing to different numbers, where we can get the highest and lowest digits of a number each time by mathematical arithmetic.

First, the number of digits is obtained, by dividing by 10*ditalnumber and 10 to obtain the highest and lowest digits respectively, to determine whether the two numbers are equal. It is important to note that the maximum number of digits needed to be removed each time a change in size. Overall, the code is relatively simple.

 class solution { Public: BOOL Ispalindrome (intx) {if(X <0)return false;if(X <Ten)return true;int Count=0;intt = x; while(t) {Count++; t = t/Ten; }intHighnum = POW (Ten,Count-1); while(Highnum >1)        {intHigh = X/highnum;intLow = x%Ten;if(High! = Low)return false;            x = X-highnum * HIGH; Highnum/= -; x = x/Ten; }return true; }};

Leetcode (+) palindrome number (palindrome digit)

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.