<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// double-ended 双端队列
class DeQueue {
#items = {}
#lowCount = 0
#count = 0
// 出队
removeFront(){
if(this.isEmpty()) return undefined
let res = this.#items[this.#lowCount]
delete this.#items[this.#lowCount]
this.#lowCount++
return res
}
// 入队
addBack(data){
this.#items[this.#count] = data
this.#count++
}
addFront(data){
// 1 如果为空
if(this.isEmpty()) {
this.addBack(data)
} else {
// 2 lowcount > 0
if(this.#lowCount > 0){
this.#lowCount--
this.#items[this.#lowCount] = data
} else {
// 2 lowcount = 0
for(let i=this.#count;i>0;i--){
this.#items[i] = this.#items[i-1]
}
this.#items[0] = data
this.#count++
}
}
}
removeBack(){
if(this.isEmpty()) {
return undefined
}
this.#count--
const res = this.#items[this.#count]
delete this.#items[this.#count]
return res
}
// 队头
peekFront(){
return this.#items[this.#lowCount]
}
peekBack(){
if(this.isEmpty()) {
return undefined
}
return this.#items[this.#count - 1]
}
isEmpty(){
return this.size() === 0
}
size() {
return this.#count - this.#lowCount
}
clear() {
this.#items = {}
this.#lowCount = 0
this.#count = 0
}
toString(){
let str = ''
for(let i = this.#lowCount;i<this.#count;i++){
str += `${this.#items[i]} `
}
return str
}
}
// dad
// 假似真时真似假
function test(str){
const lowstr = str.toLocaleLowerCase().split(' ').join('')
let dequeue = new DeQueue()
for(let i=0;i<lowstr.length;i++){
dequeue.addBack(lowstr[i])
}
console.log(dequeue)
let isEqual = true
while(dequeue.size() > 1){
if(dequeue.removeFront() !== dequeue.removeBack()){
isEqual = false
break
}
}
return isEqual
}
// let str = 'd a A d t d a a D'
let str = '假似真时真似假'
console.log( test(str))
</script>
</body>
</html>
我是Eric