Python用キュー実装スタック:
14750 ワード
キューを使用してスタックを実装するには、次の手順に従います. push(x)-要素xスタック pop()–スタックトップ要素 を除去 top()–スタックトップ要素 を取得 empty()-戻りスタックが空であるかどうか push(x) – Push element x onto stack. pop() – Removes the element on top of the stack. top() – Get the top element. empty() – Return whether the stack is empty. 例: キューの基本的な操作、すなわちpush to back、peek/pop from front、size、is emptyの操作しか使用できません. あなたが使用している言語はキューをサポートしていないかもしれません.listまたはdeque(両端キュー)を使用して、標準的なキュー操作であればキューをシミュレートできます. すべての操作が有効であると仮定できます(たとえば、空のスタックに対してpopまたはtop操作は呼び出されません)Notes: You must use only standard operations of a queue – which means only push to back, peek/pop from front, size, and is empty operations are valid. Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue. You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack). 解題構想:方法1(2つのキュー): キューが先に入ってから出て、スタックが後進して先に出ます.スタックをキューで実装すると、2つのキューで問題解を完了できます.スタックに入るときにqueue 1でノードを格納します.スタックを出るときqueue 1内のノードは順番にキューを出てqueue 2に並び、queue 1が最後の要素が残るまでスタックトップ要素であり、ポップアップすればよい.1つのtopポインタでスタックトップ要素を常に指し、top()メソッドでスタックトップ要素を問合せたときに直接topポインタを返すとよい.
メソッド2(キュー):
1つのキューのみで、スタックに入るときにキューを反転するだけです.
Java:
方法1
エンキューのたびにキューを反転すればよく、エンクロージャだけが特殊な操作を必要とし、エンクロージャ、スタックトップ要素、空かどうかはキューの正常なアウトキュー、キューヘッダ要素、空かどうかの方法で操作します.次に、インスタック時のコードを示します.
Python言語にはスタックとキューデータ構造がなく,配列Listまたは両端キューdequeでしか実現できない.
このようなプログラミング言語は、スタックおよびキュー自体がList、dequeによって実装される必要があるため、スタックまたはスタックによってキューを実装する必要はありません.
だからこの問題はこのような言語の中でこれはとても簡単で、カンニングと言える.
MyStack stack = new MyStack();
stack.push(1);
stack.push(2);
stack.top(); // 2
stack.pop(); // 2
stack.empty(); // false
注意:メソッド2(キュー):
1つのキューのみで、スタックに入るときにキューを反転するだけです.
queue
1 :queue:1
0 :queue:1
2 queue:1->2
1 :
queue:1->2 --> queue:2->1
2 queue:2->1->3
2 :
queue:2->1->3 ---> queue:1->3->2 ---> queue:3->2->1
......
これでいつでも行列ができる順番は出桟の順番になります.Java:
方法1
class MyStack {
Queue<Integer> queue1;
Queue<Integer> queue2;
private int top;//
public MyStack() {
queue1 = new LinkedList<>();
queue2 = new LinkedList<>();
}
public void push(int x) {
queue1.offer(x);
top = x;//
}
public int pop() {
while (queue1.size() > 1) {// 1
top = queue1.poll();// top , ,top
queue2.add(top);
}
// 1 2
Queue<Integer> tmp = queue2;
queue2 = queue1;
queue1 = tmp;
return queue2.poll();// 2 , 2
}
public int top() {
return top;
}
public boolean empty() {
return queue1.isEmpty();// 1
方法2:エンキューのたびにキューを反転すればよく、エンクロージャだけが特殊な操作を必要とし、エンクロージャ、スタックトップ要素、空かどうかはキューの正常なアウトキュー、キューヘッダ要素、空かどうかの方法で操作します.次に、インスタック時のコードを示します.
Queue<Integer> queue = new LinkedList<>();
public void push(int x) {
queue.add(x);
int sz = queue.size();//
while (sz > 1) {//
queue.add(queue.remove());//
sz--;
}
}
Python:Python言語にはスタックとキューデータ構造がなく,配列Listまたは両端キューdequeでしか実現できない.
このようなプログラミング言語は、スタックおよびキュー自体がList、dequeによって実装される必要があるため、スタックまたはスタックによってキューを実装する必要はありません.
だからこの問題はこのような言語の中でこれはとても簡単で、カンニングと言える.
class MyStack:
def __init__(self):
self.stack = []
def push(self, x: int) -> None:
self.stack.append(x)
def pop(self) -> int:
return self.stack.pop(-1)
def top(self) -> int:
return self.stack[-1]
def empty(self) -> bool:
return not self.stack