From ea952085f8e3c8d8a4a62a5b0bb15859f7ec3770 Mon Sep 17 00:00:00 2001 From: huangge1199 Date: Thu, 1 Apr 2021 14:56:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=9B=E6=89=A3=EF=BC=9A=E5=89=91=E6=8C=87?= =?UTF-8?q?=20Offer=2009:=E7=94=A8=E4=B8=A4=E4=B8=AA=E6=A0=88=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E9=98=9F=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/YongLiangGeZhanShiXianDuiLieLcof.java | 75 +++++++++++++++++++ .../cn/YongLiangGeZhanShiXianDuiLieLcof.md | 27 +++++++ 2 files changed, 102 insertions(+) create mode 100644 LeetCode/src/main/java/leetcode/editor/cn/YongLiangGeZhanShiXianDuiLieLcof.java create mode 100644 LeetCode/src/main/java/leetcode/editor/cn/YongLiangGeZhanShiXianDuiLieLcof.md diff --git a/LeetCode/src/main/java/leetcode/editor/cn/YongLiangGeZhanShiXianDuiLieLcof.java b/LeetCode/src/main/java/leetcode/editor/cn/YongLiangGeZhanShiXianDuiLieLcof.java new file mode 100644 index 0000000..caead1e --- /dev/null +++ b/LeetCode/src/main/java/leetcode/editor/cn/YongLiangGeZhanShiXianDuiLieLcof.java @@ -0,0 +1,75 @@ +//用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 appendTail 和 deleteHead ,分别完成在队列尾部插入整数和在队列头部删除整数的 +//功能。(若队列中没有元素,deleteHead 操作返回 -1 ) +// +// +// +// 示例 1: +// +// 输入: +//["CQueue","appendTail","deleteHead","deleteHead"] +//[[],[3],[],[]] +//输出:[null,null,3,-1] +// +// +// 示例 2: +// +// 输入: +//["CQueue","deleteHead","appendTail","appendTail","deleteHead","deleteHead"] +//[[],[],[5],[2],[],[]] +//输出:[null,-1,null,null,5,2] +// +// +// 提示: +// +// +// 1 <= values <= 10000 +// 最多会对 appendTail、deleteHead 进行 10000 次调用 +// +// Related Topics 栈 设计 +// 👍 212 👎 0 + +package leetcode.editor.cn; + +import java.util.Stack; + +//剑指 Offer 09:用两个栈实现队列 +public class YongLiangGeZhanShiXianDuiLieLcof { + public static void main(String[] args) { + //测试代码 +// Solution solution = new YongLiangGeZhanShiXianDuiLieLcof().new Solution(); + } + + //力扣代码 + //leetcode submit region begin(Prohibit modification and deletion) + class CQueue { + Stack stack1; + Stack stack2; + + public CQueue() { + stack1 = new Stack<>(); + stack2 = new Stack<>(); + } + + public void appendTail(int value) { + stack1.push(value); + } + + public int deleteHead() { + if (stack2.isEmpty()) { + while (!stack1.isEmpty()) { + stack2.push(stack1.pop()); + } + } + return stack2.isEmpty() ? -1 : stack2.pop(); + } + } + +/** + * Your CQueue object will be instantiated and called as such: + * CQueue obj = new CQueue(); + * obj.appendTail(value); + * int param_2 = obj.deleteHead(); + */ +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/LeetCode/src/main/java/leetcode/editor/cn/YongLiangGeZhanShiXianDuiLieLcof.md b/LeetCode/src/main/java/leetcode/editor/cn/YongLiangGeZhanShiXianDuiLieLcof.md new file mode 100644 index 0000000..0d7d803 --- /dev/null +++ b/LeetCode/src/main/java/leetcode/editor/cn/YongLiangGeZhanShiXianDuiLieLcof.md @@ -0,0 +1,27 @@ +

用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 appendTaildeleteHead ,分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素,deleteHead 操作返回 -1 )

+ +

 

+ +

示例 1:

+ +
输入:
+["CQueue","appendTail","deleteHead","deleteHead"]
+[[],[3],[],[]]
+输出:[null,null,3,-1]
+
+ +

示例 2:

+ +
输入:
+["CQueue","deleteHead","appendTail","appendTail","deleteHead","deleteHead"]
+[[],[],[5],[2],[],[]]
+输出:[null,-1,null,null,5,2]
+
+ +

提示:

+ +
    +
  • 1 <= values <= 10000
  • +
  • 最多会对 appendTail、deleteHead 进行 10000 次调用
  • +
+
Related Topics
  • 设计
  • \n
  • 👍 212
  • 👎 0
  • \ No newline at end of file