Open Guyidingsanyu opened 3 years ago
/**
* @param {number} x
* @return {boolean}
*/
var isPalindrome = function(x) {
const str = String(x)
const middleIndex = parseInt(str.length / 2)
for (let i = 0; i < middleIndex; i++) {
if (str[i] !== str[str.length - 1 - i]) {
return false
}
}
return true
};
/**
* @param {number} x
* @return {boolean}
*/
var isPalindrome = function(x) {
if (x < 0) {
return false
} else if (x < 10) {
return true
} else {
let arr = x.toString().split('')
arr = arr.reverse().join('')
arr = parseInt(arr)
if (arr === x) {
return true
}
return false
}
};
/**
* @param {number} x
* @return {boolean}
*/
var isPalindrome = function(x) {
if (x < 0) return false
x += ''
var left = 0
var right = x.length - 1
while (left < right) {
if (x[left] !== x[right]) {
return false
}
left++
right--
}
return true
};
leetcode - 回文字符串