Zero title: algorithm (leetode, with mind mapping + all solutions) 300 questions (9) palindrome number
Takeaway:
A Description of the topic
Overview of Two Solutions (Mind Mapping)
Three complete solutions
Scheme 1
1) code:
var isPalindrome = function(x) {
Return false if x is negative
if (x < 0) {
return false;
}
// 2) if x is not negative, convert x to a string, invert it, and then see if x is not equal to the previous value
return parseInt((x + ' ').split(' ').reverse().join(' ')) === x;
};
Copy the code
Scheme 2
1) code:
var isPalindrome = function(x) {
// Note: tempX is used for traversal
let tempX = x,
resX = 0;
Return false if x is negative
if (x < 0) {
return false;
}
// 2) if (tempX=x) and (tempX=x) is not negative, then (tempX=x), then (resX =x) is reversed
while (tempX) {
resX = (resX * 10) + (tempX % 10);
ParseInt = parseInt = parseInt = parseInt = parseInt
tempX = parseInt(tempX / 10);
}
// 3) return resX === x
return resX === x;
}
Copy the code
3 solution 3
1) code:
var isPalindrome = function(x) {
const strX = x + ' ',
l = strX.length;
Return false if x is negative
if (x < 0) {
return false;
}
// 2) For non-negative numbers, strX = x + ", iterates strX to see if the characters in the corresponding positions are the same.
// core: return false if different, return true otherwise
for (let i = 0; i < parseInt(l/2); i++) {
if(strX[i] ! == strX[(l -1) - i]) {
return false; }}return true;
}
Copy the code