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)