Optimize
This commit is contained in:
@@ -2,18 +2,19 @@
|
||||
|
||||
public class MyQueue
|
||||
{
|
||||
private Stack<int> _sInput = new Stack<int>();
|
||||
private Stack<int> _sOutput = new Stack<int>();
|
||||
private Stack<int> _sInput = new Stack<int>(256);
|
||||
private Stack<int> _sOutput = new Stack<int>(256);
|
||||
|
||||
public void Push(int x)
|
||||
{
|
||||
while (_sOutput.TryPop(out int res))
|
||||
_sInput.Push(res);
|
||||
_sInput.Push(x);
|
||||
}
|
||||
|
||||
public int Pop()
|
||||
{
|
||||
if (_sOutput.Count > 0)
|
||||
return _sOutput.Pop();
|
||||
|
||||
while (_sInput.TryPop(out int res))
|
||||
_sOutput.Push(res);
|
||||
return _sOutput.Pop();
|
||||
@@ -21,6 +22,9 @@ public class MyQueue
|
||||
|
||||
public int Peek()
|
||||
{
|
||||
if (_sOutput.Count > 0)
|
||||
return _sOutput.Peek();
|
||||
|
||||
while (_sInput.TryPop(out int res))
|
||||
_sOutput.Push(res);
|
||||
return _sOutput.Peek();
|
||||
@@ -28,8 +32,6 @@ public class MyQueue
|
||||
|
||||
public bool Empty()
|
||||
{
|
||||
while (_sInput.TryPop(out int res))
|
||||
_sOutput.Push(res);
|
||||
return _sOutput.Count == 0;
|
||||
return _sOutput.Count == 0 && _sInput.Count == 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,11 @@ internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello, World!");
|
||||
int x = 5;
|
||||
MyQueue obj = new MyQueue();
|
||||
obj.Push(x);
|
||||
int param_2 = obj.Pop();
|
||||
int param_3 = obj.Peek();
|
||||
bool param_4 = obj.Empty();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user