Day 3: Question 9, palindrome number:
Address: leetcode-cn.com/problems/pa…
If a negative number is greater than zero, it will be false. If a negative number is greater than zero, it will be true. If a negative number is greater than zero, it will be true.
var isPalindrome = function(x) {
let a = x;
If (a<0)// Start with the negative numbers and 0
{return false; }
else if(a==0)
{return true; }
let b = []; // declare the array
for(let i = 0; a>0; I++)// loop to mod numbers into arrays
{
b[i] = a%10;
a=Math.floor(a/10);
// Because js does not include integers and floating-point types, it is necessary to use the Math method to round.
// Because js does not include integers and floating-point types, it is necessary to use the Math method to round.
}
for(let i = 0; i < b.length-1 ; I++)// loop judgment
{
if(b[i]! =b[b.length-1-i])
{
return false;
}
}
return true;
};
Execution time: 240 ms, beating 58.77% of all JavaScript commits
Memory consumption: 46.2 MB, beating 60.91% of all JavaScript commits
It’s probably best not to do it, but find a super compact code to do it.
var isPalindrome = function(x)
{ return x.toString() == x.toString().split(“”).reverse().join(“”); }
ToString is converted to a string, split is converted to an array, reverse is flipped, join is converted to a string, and then compared with the previous string, the result is true or false, return, really concise.