Title:
Determine whether an integer is a palindrome. Do this without extra space.
Analysis:
The topic originates from Leetcode. palindrome string is a literal and anti-read all the same strings, such as "level" or "noon" and so on is a palindrome string. Of course, the palindrome string is similar in integer form. But negative numbers are not palindrome strings. Two ways of thinking:
- By definition, the end of the string is compared to 1 or 0 elements in the middle (depending on whether the integer is an odd or even number of digits). The advantage is that when it is not a palindrome, it can be quickly discovered. If it is a palindrome, you must compare the entire number of digits.
- According to the characteristics of palindrome string, the inverse sequence, the number is the same. The advantage is not to be judged by the number of digits.
Code:
<span style= "Font-size:12px;font-weight:normal;" >class Solution {Public:bool ispalindrome (int x) {vector<int> v;int I, j;if (x = = 0) return True;while (x) {V.push_ Back (x%); x/= 10;} i = 0;j = V.size () -1;while (i < J) {if (v.at (i++)! = v.at (j--)) return false;} return true;}}; </span>
Class Solution {Public:bool ispalindrome (int x) {int y = 0;int T = x;if (T < 0)//negative number is not a palindrome return False;while (t) {y = y * Ten + t% 10;t/= 10;} if (y ^ x)//Determine if the inverse order is equal to return False;return true;}};
Palindrome number (Palindrome string)