" Questions Eyes " Leetcode (9 ) : palindrome number
url:https://leetcode.com/problems/palindrome-number/
Description
Determine whether an integer is a palindrome. Do this without extra space.
"Chinese description"
Determines whether an integer is a palindrome number, similar to 121, 2332, 1000001, 22. doing this does not apply to extra space.
————————————————————————————————————————————————————————————
"Initial thinking"
I just want to kill this problem!! In the Chinese description, I specifically wrote the title in red font requirements, is that the request tortured me for nearly a day!!
Do this without extra space. What's the meaning of this sentence? If I don't understand it correctly, I can't use extra space!
What is extra space? I can understand that, in addition to X, other applications can be counted as extra space. Even a pointer, is in the stack Occupy space Ah! This means that you can't turn an integer into a string and not know the number of digits in the integer, and all of your code can only use one variable, which is the X.
Reasoning, thought for a whole day without fruit. Finally, after dinner, no way, the end of the rope, I thought an easy problem incredibly I made it! Go back home, don't think about Google, Facebook. I looked at someone else's answer and I was crazy! Obviously useful extra space Ah! One person still in the discuss asked: "I this question does not use the extra space?" Why is it AC? "It seems that everyone is as troubled as I am!"
Well, if you can use extra space, the problem is really too simple. Not much analysis, directly on the code, case closed!
"Show me the Code!!! 】
1 Public Static BooleanIspalindrome (intx) {2 if(x < 0) {return false;}//negative numbers are definitely not, return directly3 if(x >= 0 && x < 10) {return true;}//0-9 must be, direct return4 5 //Positive Conditions6String str =string.valueof (x);7 8 intCount = 0;9 inti = 0, j = str.length ()-1;Ten while(Count < Str.length ()/2) {//palindrome number, only need to match on both sides of the can. When odd digits are dropped, the middle match is removed. One if(Str.charat (i) = =Str.charat (j)) { Acount++; -i++; -j--; the}Else { - return false;//immediately returns false once a mismatch is found - } - } + return true; -}
Ispalindrome
Reflection
This question let me realize a problem, really can not think out of the topic or hurriedly see the hint to see discuss bar, after all, after all, interview with the interviewer to communicate to tip. It's only a waste of time to end up dead!
Leetcode (9): palindrome number