Determine if a number is a palindrome

Source: Internet
Author: User

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

Determines whether a number is a palindrome number, and does not use additional storage space.

"Palindrome" is to read the anti-reading can read through the sentence, then palindrome number, it is easy to understand, that is, a number of positive and negative reading value is the same. There is also a requirement to not use additional storage space.

Hints:
To compare the highest bit of a number to the lowest bit, it is easy to take the lowest bit of a number (X%10), but it is difficult to get a high position.
Solution 1:
First you need to know a negative number of processing methods, such as 1 is not a palindrome, where we think negative numbers are not palindrome number.
Intuitively speaking, judging a palindrome number can reverse the number, as follows:

intreverse(int num) {  0);   // 只考虑非负数  int0;  while0) {    rev = rev*10 + num%10;    10;  }  return rev;}

, and then compared to the original number, the algorithm uses an additional storage space, and does not take into account the problem of cross-border, for example, in C + + int_max=2^31-1 = 2147483647 palindrome number 7463847412 is out of bounds, so this method is not a good method.

Solution 2:
You can also convert a number to a string, determine whether a string is a palindrome, the method needs to be the same number of bytes with the same length of memory space, is not a 0 storage space algorithm.

String Int2str (intx) {stringret;Ifx==0)ret. Push_back (' 0 ');Whilex)    {ret. Push_back (' 0 '+x%Ten);        x/=Ten;} reverse (ret. Begin(),ret. End()); Reverse StringReturnret;}

Solution 3:
The idea of using high-and low-level equality does not cross the border, but requires additional storage space.

BOOL Ispalindrome (intx) {if(X <0)return false;int Div=1; while(X/Div>=Ten) {Div*=Ten;//div has a maximum bit of 1 and the same length as x} while(X! =0) {inth = x/Div;the highest bit of//x    intL = x%Ten;lowest bit of//x    if(H! = L)return false; x = (x%Div) /Ten;//Remove the highest and lowest digits of x    Div/= -; }return true;}

Solution 4:
0 storage methods. The use of recursive thinking, the use of stack memory, to achieve the purpose of 0 storage.

BOOLIspalindrome (intXint&y) {//y is a variable that must be a reference and a pointer type that can be changed  if(X <0)return false;if(x = =0)return true;if(Ispalindrome (x/Ten, y) && (xTen= = y%Ten)) {//Every time I execute to x%10 = = y%10, x is the first I bit, y means the front (n+1-i) bitY/=Ten;return true; }Else{return false; }}BOOLIspalindrome (intx) {returnIspalindrome (x, x);}

Determine if a number is a palindrome

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.