Palindrome Number (input string)
Question:
Determine whether an integer is a palindrome. Do this without extra space.
Analysis:
This question is from leetcode. A text return string is a string that is the same for both the forward and reverse reads. For example, "level" or "noon" is a text return string. Of course, the return strings in integer form are similar. But the negative number is not a return string. Two ideas:
By definition, compare the beginning and end of a string in sequence until one or zero elements are separated (depending on whether the integer is an odd or even number of digits ). The advantage is that it can be quickly discovered when it is not a text string. For a return string, you must compare all digits. According to the characteristics of the input string, the numbers are the same in positive and negative order. The advantage is that it does not have to be determined by the number of digits. Code:
Class Solution {public: bool isPalindrome (int x) {vector
V; int I, j; if (x = 0) return true; while (x) {v. push_back (x % 10); x/= 10;} I = 0; j = v. size ()-1; while (I <j) {if (v. at (I ++ )! = V. at (j --) return false;} return true ;}};
Class Solution {public: bool isPalindrome (int x) {int y = 0; int t = x; if (t <0) // negative number is not the return false; while (t) {y = y * 10 + t % 10; t/= 10;} if (y ^ x) // judge whether the positive and negative orders are equal return false; return true ;}};