palindrome
回文
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-05-25
* @modified
*
* @description palindrome 回文 recursive
* @augments
* @example
* @link
*
*/
const log = console.log;
const palindromeChecker = (str = ``) => {
const len = str.length;
if(len <= 1) {
return true;
} else {
return str === Array.from(str).reverse().join(``);
}
};
// test
const t1 = palindromeChecker(`123`);
const t2 = palindromeChecker(`11`)
const t3 = palindromeChecker(`12`)
const t4 = palindromeChecker(`12345`)
const t5 = palindromeChecker(`12321`)
log(`t1`, t1)
log(`t2`, t2)
log(`t3`, t3)
log(`t4`, t4)
log(`t5`, t5)
// t1 false
// t2 true
// t3 false
// t4 false
// t5 true
// export default palindromeChecker;
// export {
// palindromeChecker,
// };
performance optimization
const isPalindrome = (str = ``) => {
const len = Math.floor(string.length / 2);
str = str.toLocaleLowerCase();
for (let i = 0; i < len; i++) {
if (str[i] !== str[len - i - 1]) {
// 提前结束
return false;
}
}
return true;
}
©xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!