From 03a07e4fe321f79c94482732b62e0aec08d53573 Mon Sep 17 00:00:00 2001 From: Omooo <869759698@qq.com> Date: Sun, 28 Jun 2020 09:46:23 +0800 Subject: [PATCH] =?UTF-8?q?Create=20=E6=A0=88=E7=9B=B8=E5=85=B3.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- blogs/Algorithm/剑指 Offer/栈相关.md | 35 ++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 blogs/Algorithm/剑指 Offer/栈相关.md 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(); + } + } +} +``` +