diff --git a/blogs/Algorithm/剑指 Offer/栈相关.md b/blogs/Algorithm/剑指 Offer/栈相关.md new file mode 100644 index 0000000..e56869d --- /dev/null +++ b/blogs/Algorithm/剑指 Offer/栈相关.md @@ -0,0 +1,35 @@ +--- +栈相关 +--- + +#### [09. 用两个栈实现队列](https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/) + +```java +class CQueue { + + Stack putStack, takeStack; + + public CQueue() { + putStack = new Stack<>(); + takeStack = new Stack<>(); + } + + public void appendTail(int value) { + putStack.push(value); + } + + public int deleteHead() { + if (takeStack.isEmpty()) { + while (!putStack.isEmpty()) { + takeStack.push(putStack.pop()); + } + } + if (takeStack.isEmpty()) { + return -1; + } else { + return takeStack.pop(); + } + } +} +``` +