Leetcode 9. Determines whether an integer is a palindrome number

Source: Internet
Author: User

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

Note: The number of return is the same as read and read, such as 121,234432

Problem Solving Analysis:

You need to be careful.
(1) Negative number is not a return
(2) Single digit is the number of returns
Ideas:
(1) Use the reverse of the integer in the previous question, the reverse of a number, and then compare with the original value: The problem is that there may be overflow, need to calculate each bit, impact efficiency, there is room for improvement
(2) comparing the highest and lowest positions , unequal jumps, equal compared to the second high and low, recursive end: efficient, but the difficulty is how to obtain the highest and Qiatouquwei .

Program implementation

(1) Reverse of integer

bool isPalindrome(int x) {    longlong0;    int t = x;    for010)        y = y*10 + t %10;    if(x == y)        returntrue;    returnfalse//如果溢出,肯定不等}

(2) Compare the highest and lowest positions one by one

BOOLIspalindrome (intx) {if(X <0)return false;intD =1; while(x/d >=Ten)//Calculate the divisor required to take the highest bitD *=Ten; while(x) {intHigh_bit = x/d;intLow_bit = x%Ten;if(High_bit! = low_bit) Break; x = x% d/Ten;//* Qiatouquwei *D = d/ -;//Kick out 2 bits at a time}if(x)return false;return true; }};

Leetcode 9. Determines whether an integer is a 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.