Leetcode------palindrome Number

Source: Internet
Author: User

Title: Palindrome number
Pass Rate: 29.1%
Difficulty: Simple

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

This question does not understand is because the word does not know, then is not knowing what is a palindrome number, translation, Baidu to know what is a palindrome number, if there is no title on the last word, then still good to do, the given number into a string, and then from the 0th position compared to the last position, altogether compared length/ 2 times.

The problem is that you can't use extra space that means you can't convert to a string. Which general idea or before and after the comparison, first to calculate the length of the number, with the exception of 10 operation, to obtain length so still need to compare LENGHT/2 times, the problem has to be illustrated by an example:

If a palindrome number is: 1234321, the length of 7, then LENGTH/2 is 3, that is, compare the <0,6>;<1,5>;<2,4> three numbers if the same is palindrome number.

If the palindrome number is 123321,LENGTH/2 still 3, that is, the comparison <0,5>;<1,4>;<2,3> three group of numbers are the same as palindrome.

The solution to this problem is not to use an extra array to get a count. To think about the redundancy operation, let's take the 1234321 figure as an example,

To compare the location of <0,6>;<1,5>;<2,4>,length to 7, we found that,<0,6> is <0,length-1-0>,<1,5> is <1, Length-1-1>

That is <i,length-1-i>, then for the first time in 1234321 the comparison is the 0th position of the 1 and fifth positions of 1, for the 0th position 1,1234321%1 (10^6) obtained, sixth position 1,1234321% (10^0) , said here to understand. A getnum function can be used to obtain

See the code directly below:

1  Public classSolution {2      Public BooleanIspalindrome (intx) {3         intcount=1,num=x;4         if(x<0)return false;5          while(num/10!=0){6num/=10;7count++;8         }9          for(inti=0;i<count/2;i++){Ten             intA=i; One             intB=count-1-i; A             if(Getnum (x,a)! =Getnum (x,b)) { -                 return false; -             } the              -         } -         return true; -     } +      Public intGetnum (intXinti) { -         intTmp= (int) Math.pow (10, i); +         return(x/tmp)%10; A     } at}

Leetcode------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.