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