1 Star 0 Fork 0

ccyy-阿亮/datastructures_ts

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
palindrome-checker.ts 933 Bytes
一键复制 编辑 原始数据 按行查看 历史
import Deque from '../data-structures/deque';
/**
* 用双端队列实现回文检查器
* @param aString 目标字符串
*/
export function palindromeChecker(aString: string) {
if (aString === undefined || aString === null ||
(aString !== null && aString.length === 0)) {
return false;
}
const deque = new Deque<string>();
const lowerString = aString.toLocaleLowerCase().split(' ').join(''); // 小写化,去除空格
let firstChar: string;
let lastChar: string;
for (let i = 0; i < lowerString.length; i++) {
deque.addBack(lowerString.charAt(i)); // 依次存入双端队列中
}
while (deque.size() > 1) { // 可能剩中间一个字符
firstChar = deque.removeFront(); // 从首端取一个
lastChar = deque.removeBack(); // 从尾端取一个
if (firstChar !== lastChar) {
return false;
}
}
return true;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
TypeScript
1
https://gitee.com/liawnliu/datastructures_ts.git
git@gitee.com:liawnliu/datastructures_ts.git
liawnliu
datastructures_ts
datastructures_ts
master

搜索帮助