"title" Determine whether an integer is a palindrome. Do this without extra space.
Click to show spoilers.
Some hints:
Could negative integers be palindromes? (ie,-1)
If you is thinking of converting the integer to string, note the restriction of using extra space.
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?
There is a more generic the solving this problem.
Solution: Divide the number of int into one, all the elements in a vector, and then determine whether the vector is symmetrical or not.
Note: The use of push_back added elements in vectors, vectors can be directly used in the following table to take the number.
The code is as follows:
Class Solution {public: bool Ispalindrome (int x) { if (x < 0) return false; Vector<int> nums; while (x) { nums.push_back (x%10); x/=; } int len = Nums.size (); for (int i = 0; i < LEN/2; i++) { if (nums[i]! = nums[len-1-i]) % subscript pay attention to return false; } return true; }};
Palindrome number--Problem Solving notes