Determine whether an integer is a palindrome. Do this without extra space.
Detect whether the current number is a palindrome number, while not adding extra memory space, here is a note that negative numbers are not likely to be palindrome numbers
And then it detects each digit to compare it.
The code is still very tedious to write, the main point is that the number of digits is the number of the base and even the number of times the process of processing is different
Class Solution {Public:int geth (int x,int num)//Gets the corresponding position of the number {while (num) {x = x/10;num--;} return x%10;} BOOL Ispalindrome (int x) { if (x < 0) { return false; } int hnum = 0;int y = x;while (y) {hnum++;//record number y = Y/10;} if (hnum&1)//< odd digits {int num = Hnum/2;//<dint i = 1;while (i <= num) {int h = geth (x,num-i); int L = Geth (x,num+i); if (h!= l) {return false;} i++;}} Else{int num = Hnum/2;int i = 0;while (i < num) {int h = geth (x,num+i); int L = Geth (x,num-1-i); if (h!= l) {return false;} i++;}} return true;}};
Or put out some of the better solution on the Internet:
Here we use a base to calculate the highest level directly, 100, 1000,100000 ... when you do, use division directly.
And the comparison here is from the two sides to the middle of the comparison, then this time the odd bit position is no longer important, you do not need to write branches
So: If it's a comparison between digits, it's best to compare it with the tail.
Class Solution {public: bool Ispalindrome (int x) { //Start Typing your C + + solution below //do not write I NT Main () function if (x < 0) return false; if (x = = 0) return true; int base = 1; while (X/base >=) base *=; while (x) { int leftdigit = x/base; int rightdigit = x%; if (leftdigit! = Rightdigit) return false; X-= base * LEFTDIGIT; Base/=; x/=; } return true; }};
Leetcode-palindrome number number is a palindrome