We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
队列是先进先出,而栈呢,是先进的后出,但是用两个栈,把第一个栈的值压入第二个栈,先进的第一个栈的值就跑到了第二个栈的最顶端了。 由此就可以模拟出先进先出的效果。 例如,所有push操作都往stack1里面存放,当有pop操作的时候,优先考虑stack2是否有值,有的话直接pop,没有就将stack1中的值压入stack2中,然后pop。
The text was updated successfully, but these errors were encountered:
No branches or pull requests
题目描述
用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
思路
队列是先进先出,而栈呢,是先进的后出,但是用两个栈,把第一个栈的值压入第二个栈,先进的第一个栈的值就跑到了第二个栈的最顶端了。 由此就可以模拟出先进先出的效果。
例如,所有push操作都往stack1里面存放,当有pop操作的时候,优先考虑stack2是否有值,有的话直接pop,没有就将stack1中的值压入stack2中,然后pop。
The text was updated successfully, but these errors were encountered: