This is my third article about getting started
preface
Today, suddenly it's Friday, that Friday inertia kicks in... So, don't want to write my bug, leetcode brush a problem? I was going to do it randomly, and then, uh, leetcode did me a favor and I clicked a random question a few times in a row... Well, one is more difficult than one, and then even the topic I can not understand, well, I do not know whether everyone is so, well, I should be more think, should be my little cabbage is so. So, I click difficulty selection, -> easy, well, just like that, refresh the list, pick a look at the most confused topic, well, it is, palindrome number (is my own too dish, just look at the topic, do not know what this is a number, so, click enter).Copy the code
The title
Given an integer x, return true if x is a palindrome integer. Otherwise, return false. Palindromes are integers that read in positive (left to right) and backward (right to left) order. For example, 121 is palindrome and 123 is not.Copy the code
Like this topic, the explanation is very clear, not so that I can not understand the topic is what, ha ha **** (no way, after all, he is a cabbage)
My train of thought
After understanding the title, the judgment of palindrome number and my interpretation, palindrome number is just like the center symmetry in the graph. After folding in half, it is completely aligned, so my brain starts to start slowly. First of all, I'm going to rule out the special ones, like 0,100,110,120, and then I'm going to start with the other ones. But I didn't see the pattern of the numbers, and then all of a sudden I had a string in my head, and so I started toStirng, and I thought, "Turn it into a string of fields, and I cut off the one in the middle, and the left and right arrays are the same." Later, I like a IQ is not too high people, really cut the middle number, but again encountered a problem, I split cut, that is not necessarily two arrays, may be a lot of, HMMM, after testing, the above failure, then I suddenly flash again, HMMM? Why cut into left and right arrays? Can't I just reverse the alignment? Doesn't it smell good? So my answer is ~~Copy the code
My answer:
/ * * *@param {number} x
* @return {boolean}* /
var isPalindrome = function(x) {
if(x === 0) {
return true
} else if(x < 0 || x % 10= = =0) {
return false
} else {
var numberReStr = x.toString().split(' ').reverse()
var newNum = numberReStr.join(' ')
return x.toString() === newNum
}
};
Copy the code
Well, success… But it didn’t seem easy. Well, my big head couldn’t think of anything else, so I submitted it. well
Conclusion:
1213. ToString ()(or String('23213')). 3. Array reverse 4. Array becomes string joinCopy the code
It’s all the basics, but I didn’t think of it at first, so the basics are important. Come on!