add: 回文数
This commit is contained in:
1 parent
da0e4f1359
commit
304866ecbf
3 files changed
+30
No files matched your search
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* @param {number} x
|
||||
* @return {boolean}
|
||||
*/
|
||||
export const isPalindrome = function (x) {
|
||||
if (x < 0 || (x % 10 === 0 && x !== 0)) return false // 如果末尾数为0且不是0
|
||||
|
||||
let reverse = 0
|
||||
|
||||
while (reverse < x) {
|
||||
reverse = reverse * 10 + x % 10
|
||||
x = x / 10 | 0
|
||||
}
|
||||
|
||||
return reverse === x || (reverse / 10 | 0) === x
|
||||
}
|
||||
Reference in new issue
Block a user