[Algorithms] 17. Queue From Stacks
7362 ワード
質問する

解決策


コミットコード
class Queue {
constructor() {
this.first = new Stack();
this.second = new Stack();
}
add(record) {
this.first.push(record);
}
remove() {
while (this.first.peek()) {
this.second.push(this.first.pop());
}
const record = this.second.pop();
while (this.second.peek()) {
this.first.push(this.second.pop());
}
return record;
}
peek() {
while (this.first.peek()) {
this.second.push(this.first.pop());
}
const record = this.second.peek();
while (this.second.peek()) {
this.first.push(this.second.pop());
}
return record
}
}
Reference
この問題について([Algorithms] 17. Queue From Stacks), 我々は、より多くの情報をここで見つけました https://velog.io/@peng0code/Algorithms-17.-Queue-From-Stacksテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol