-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0225-ImplementStackUsingQueues.cs
57 lines (49 loc) · 1.44 KB
/
0225-ImplementStackUsingQueues.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
//-----------------------------------------------------------------------------
// Runtime: 92ms
// Memory Usage: 23.9 MB
// Link: https://leetcode.com/submissions/detail/352421862/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _0225_ImplementStackUsingQueues
{
private readonly Queue<int> queue;
/** Initialize your data structure here. */
public _0225_ImplementStackUsingQueues()
{
queue = new Queue<int>();
}
/** Push element x onto stack. */
public void Push(int x)
{
queue.Enqueue(x);
var i = queue.Count - 1;
while (i-- > 0)
queue.Enqueue(queue.Dequeue());
}
/** Removes the element on top of the stack and returns that element. */
public int Pop()
{
return queue.Dequeue();
}
/** Get the top element. */
public int Top()
{
return queue.Peek();
}
/** Returns whether the stack is empty. */
public bool Empty()
{
return queue.Count == 0;
}
}
/**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.Push(x);
* int param_2 = obj.Pop();
* int param_3 = obj.Top();
* bool param_4 = obj.Empty();
*/
}