答案 0 :(得分:1)
您需要先检查字符串的长度,然后根据该长度执行其余操作。
// check if last element length is more than 1
if(queue[queue.length - 1].length > 1){
// remove last character and update string
queue[queue.length - 1] = queue[queue.length - 1].slice(1, -1);
} else {
// else remove the last element
queue.pop();
}
或弹出最后一个元素,检查字符串的长度是否大于1,然后删除最后一个字符并压入数组。
// pop last element from the array
let last = quene.pop();
// check if element length is more than 1
if(last.length > 1){
// remove last character from string and push to the array again
queue.push(last.slice(1, -1))
}