-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0232-ImplementQueueUsingStacks.cs
67 lines (56 loc) · 1.67 KB
/
0232-ImplementQueueUsingStacks.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//-----------------------------------------------------------------------------
// Runtime: 92ms
// Memory Usage: 24 MB
// Link: https://leetcode.com/submissions/detail/351844879/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _0232_ImplementQueueUsingStacks
{
private readonly Stack<int> s1;
private readonly Stack<int> s2;
private int s1Front;
/** Initialize your data structure here. */
public _0232_ImplementQueueUsingStacks()
{
s1 = new Stack<int>();
s2 = new Stack<int>();
}
/** Push element x to the back of queue. */
public void Push(int x)
{
if (s1.Count == 0)
s1Front = x;
s1.Push(x);
}
/** Removes the element from in front of queue and returns that element. */
public int Pop()
{
if (s2.Count == 0)
while (s1.Count > 0)
s2.Push(s1.Pop());
return s2.Pop();
}
/** Get the front element. */
public int Peek()
{
if (s2.Count > 0)
return s2.Peek();
return s1Front;
}
/** Returns whether the queue is empty. */
public bool Empty()
{
return s1.Count == 0 && s2.Count == 0;
}
}
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.Push(x);
* int param_2 = obj.Pop();
* int param_3 = obj.Peek();
* bool param_4 = obj.Empty();
*/
}