1 Star 0 Fork 0

Xiaoke/Data-structure-and-algorithm-code

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Deque.js 3.24 KB
一键复制 编辑 原始数据 按行查看 历史
Xiaoke 提交于 2022-07-15 16:25 . 初始化仓库
/**
* addFront:队列前端添加元素
* addBack:队列后端添加元素
* removeFront:队列前端移除元素
* peekBack:队列后端移除元素
* isEmpty:判断是否为空
* size:返回栈里元素的长度
* clear:清空栈里的元素
* toString:数组中的toString方法
*
* @class Deque
*/
class Deque{
constructor(){
this._count = 0;
this._lowestCount = 0;
this._items = {};
}
addFront(elem){
if(this.isEmpty()){
this.addBack(elem);
}else if(this._lowestCount > 0){
this._lowestCount --;
this._items[this._lowestCount] = elem;
}else {
for(let i = this._count; i > 0; i--){
this._items[i] = this._items[i-1];
}
this._count++;
this._lowestCount = 0;
this._items[0] = elem;
}
}
addBack(...elem){
for(let i = 0; i < elem.length; i++){
this._items[this._count] = elem[i];
this._count ++;
}
}
removeFront(){
if(this.isEmpty()){
return undefined;
}
const result = this._items[this._lowestCount];
delete this._items[this._lowestCount];
this._lowestCount ++;
return result;
}
removeBack(){
if(this.isEmpty()){
return undefined;
}
this._count --;
const result = this._items[this._count];
delete this._items[this._count];
return result;
}
peekFront(){
if(this.isEmpty()){
return undefined;
}
return this._items[this._lowestCount];
}
peekBack(){
if(this.isEmpty()){
return undefined;
}
return this._items[this._count - 1];
}
isEmpty(){
return this._count - this._lowestCount === 0;
}
size(){
return this._count - this._lowestCount;
}
clear(){
this._items = {};
this._count = 0;
this._lowestCount = 0;
}
toString(){
if(this.isEmpty()){
return '';
}
let objString = `${this._items[this._lowestCount]}`;
for(let i = this._lowestCount + 1; i < this._count; i++ )
objString = `${objString},${this._items[i]}`;
return objString;
}
}
const deque = new Deque();
console.log(deque.isEmpty()); // true
deque.addBack('john');
deque.addBack('jack');
console.log(deque.toString()); // john,jack
deque.addBack('camila');
console.log(deque.toString()); // john,jack,camila
console.log(deque.size()); // 3
console.log(deque.isEmpty()); // false
deque.removeFront();
console.log(deque.toString()); // jack,camila
deque.addFront('john');
console.log(deque.toString()); // john,jack,camila
// 回文检查器
const palindromeChecker = (aString) => {
if(aString === undefined || aString === null || (aString !== null && aString.length === 0)){
return false;
}
const deque = new Deque();
const lowerString = aString.toLocaleLowerCase().split(' ').join('');
let isEqual = true;
let fistChar, lastChat;
for(let i = 0; i < lowerString.length; i++){
deque.addBack(lowerString.charAt(i));
}
while(deque.size() > 1 && isEqual){
fistChar = deque.removeFront();
lastChat = deque.removeBack();
if(fistChar !== lastChat){
isEqual = false;
}
}
return isEqual;
}
console.log('a', palindromeChecker('a'))
console.log('abc', palindromeChecker('abc'))
console.log('aba', palindromeChecker('aba'))
// ''.split('').reverse().join('') === ''
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/xiaaoke/Data-structure-and-algorithm-code.git
git@gitee.com:xiaaoke/Data-structure-and-algorithm-code.git
xiaaoke
Data-structure-and-algorithm-code
Data-structure-and-algorithm-code
master

搜索帮助