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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user