Skip to content

Commit cbbfead

Browse files
committed
232. Implement Queue using Stacks
[Implement Queue using Stacks](https://leetcode.com/problems/implement-queue-using-stacks/description/)
1 parent 21f51a9 commit cbbfead

File tree

4 files changed

+211
-0
lines changed

4 files changed

+211
-0
lines changed

232/step1.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
3+
push:
4+
Time: O(N)
5+
Space: O(N)
6+
O(N)
7+
pop:
8+
Time: O(1)
9+
Space: O(1)
10+
peek:
11+
Time: O(1)
12+
Space: O(1)
13+
empty:
14+
Time: O(1)
15+
Space: O(1)
16+
17+
18+
### 思考の流れ
19+
20+
スタックとキューはデータの取り出し口の位置が異なるだけで同じことをしているという前提がある
21+
このことからスタックの中に、数値を逆順にしたものを入れておけばキューの動作を実現できると気づく
22+
要素群を持つスタックAから、スタックBに対して要素をすべて渡すと順序が逆になる。
23+
これを利用してスタック感の要素の受け渡しでreveseを行う
24+
25+
push時は片方のスタックに一時的に全ての要素を対比させる。
26+
空にしたスタックに対して新規要素を入れ、先ほど退避した要素をすべて戻すことで逆順を維持したまま新規要素をpushできる。
27+
残りのメソッドは特に悩む要素がないのでスタックのメソッドを用いて実装する。
28+
*/
29+
class MyQueue {
30+
public:
31+
stack<int> main;
32+
stack<int> tmp;
33+
34+
MyQueue() {}
35+
36+
void push(int x) {
37+
while (!main.empty()) {
38+
tmp.push(main.top());
39+
main.pop();
40+
}
41+
tmp.push(x);
42+
while (!tmp.empty()) {
43+
main.push(tmp.top());
44+
tmp.pop();
45+
}
46+
}
47+
48+
int pop() {
49+
int top_value = main.top();
50+
main.pop();
51+
return top_value;
52+
}
53+
54+
int peek() {
55+
return main.top();
56+
}
57+
58+
bool empty() {
59+
return main.empty();
60+
}
61+
};

232/step2.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
ロジック的な箇所はあまりいじる部分がないと感じたため変数名だけいじった
3+
*/
4+
class MyQueue {
5+
public:
6+
stack<int> primary;
7+
stack<int> temporary;
8+
9+
MyQueue() {}
10+
11+
void push(int x) {
12+
while (!primary.empty()) {
13+
temporary.push(primary.top());
14+
primary.pop();
15+
}
16+
temporary.push(x);
17+
while (!temporary.empty()) {
18+
primary.push(temporary.top());
19+
temporary.pop();
20+
}
21+
}
22+
23+
int pop() {
24+
int top_value = primary.top();
25+
primary.pop();
26+
return top_value;
27+
}
28+
29+
int peek() {
30+
return primary.top();
31+
}
32+
33+
bool empty() {
34+
return primary.empty();
35+
}
36+
};
37+
38+
/**
39+
* Your MyQueue object will be instantiated and called as such:
40+
* MyQueue* obj = new MyQueue();
41+
* obj->push(x);
42+
* int param_2 = obj->pop();
43+
* int param_3 = obj->peek();
44+
* bool param_4 = obj->empty();
45+
*/

232/step2_amortized_o1.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
解説を見て書いた別解法、平均的にはこちらのほうが計算量が優秀
3+
peekとpopでスタックの移動に関する実装重複があったのでpopの中からpeekを呼び出すようにした
4+
*/
5+
class MyQueue {
6+
public:
7+
stack<int> push_stack;
8+
stack<int> pop_stack;
9+
10+
MyQueue() {}
11+
12+
void push(int x) {
13+
while (!pop_stack.empty()) {
14+
push_stack.push(pop_stack.top());
15+
pop_stack.pop();
16+
}
17+
18+
push_stack.push(x);
19+
}
20+
21+
int pop() {
22+
int pop_value = peek();
23+
pop_stack.pop();
24+
return pop_value;
25+
}
26+
27+
int peek() {
28+
while (!push_stack.empty()) {
29+
pop_stack.push(push_stack.top());
30+
push_stack.pop();
31+
}
32+
return pop_stack.top();
33+
}
34+
35+
bool empty() {
36+
return push_stack.empty() && pop_stack.empty();
37+
}
38+
};
39+
40+
/**
41+
* Your MyQueue object will be instantiated and called as such:
42+
* MyQueue* obj = new MyQueue();
43+
* obj->push(x);
44+
* int param_2 = obj->pop();
45+
* int param_3 = obj->peek();
46+
* bool param_4 = obj->empty();
47+
*/

232/step3.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
push:
3+
Time: O(N) or O(1)
4+
Space: O(N) or O(1)
5+
O(N)
6+
pop:
7+
Time: O(N) or O(1)
8+
Space: O(N) or O(1)
9+
peek:
10+
Time: O(N) or O(1)
11+
Space: O(N) or O(1)
12+
empty:
13+
Time: O(1)
14+
Space: O(1)
15+
*/
16+
class MyQueue {
17+
public:
18+
19+
stack<int> push_stack;
20+
stack<int> pop_stack;
21+
22+
MyQueue() {}
23+
24+
void push(int x) {
25+
while (!pop_stack.empty()) {
26+
push_stack.push(pop_stack.top());
27+
pop_stack.pop();
28+
}
29+
push_stack.push(x);
30+
}
31+
32+
int pop() {
33+
int pop_value = peek();
34+
pop_stack.pop();
35+
return pop_value;
36+
}
37+
38+
int peek() {
39+
while (!push_stack.empty()) {
40+
pop_stack.push(push_stack.top());
41+
push_stack.pop();
42+
}
43+
return pop_stack.top();
44+
}
45+
46+
bool empty() {
47+
return pop_stack.empty() && push_stack.empty();
48+
}
49+
};
50+
51+
/**
52+
* Your MyQueue object will be instantiated and called as such:
53+
* MyQueue* obj = new MyQueue();
54+
* obj->push(x);
55+
* int param_2 = obj->pop();
56+
* int param_3 = obj->peek();
57+
* bool param_4 = obj->empty();
58+
*/

0 commit comments

Comments
 (0)