国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

不得已二 Java兩個棧模擬一個隊列

2019-11-14 11:01:54
字體:
來源:轉載
供稿:網友

github:https://github.com/cbamls 歡迎加入 (java)全國各校IT精英 QQ群號: 467123855

很簡單只要記住add操作不需要倒棧 poll 和peek的時候判斷性的倒棧 肯定能設計出來

package com.offer.chapter1;import java.util.Stack;/** * CopyRright (c)2014-2016 Haerbin Hearglobal Co.,Ltd * PRoject: demo * Comments: * Author:cbam * Create Date:2017/2/4 * Modified By: * Modified Date: * Modified Reason: */public class Problem_02_TwoStacksQueue { private Stack<Integer> stackPush; private Stack<Integer> stackPop; public Problem_02_TwoStacksQueue() { stackPush = new Stack<>(); stackPop = new Stack<>(); } public void add(int pushInt) { stackPush.push(pushInt); } public int poll() { if(stackPop.empty() && stackPush.empty()) { throw new RuntimeException("Queue is empty"); } else if(stackPop.empty()) { while(!stackPush.empty()) { stackPop.push(stackPush.pop()); } } return stackPop.pop(); } public int peek() { if(stackPop.empty() && stackPush.empty()) { throw new RuntimeException("Queue is empty"); } else if(stackPop.empty()) { while(!stackPush.empty()) { stackPop.push(stackPush.pop()); } } return this.stackPop.peek(); }}
上一篇:單詞接龍

下一篇:Java獲取鍵盤輸入值

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 肇州县| 泗洪县| 康乐县| 新竹市| 通渭县| 石阡县| 顺义区| 琼中| 抚松县| 南通市| 沙洋县| 乡宁县| 任丘市| 清苑县| 赤水市| 仪征市| 天镇县| 龙胜| 岳池县| 新平| 汝州市| 万山特区| 南开区| 舞钢市| 浠水县| 商丘市| 贺州市| 汕尾市| 金川县| 大洼县| 佛坪县| 西峡县| 扶风县| 赣州市| 安塞县| 班戈县| 浦北县| 怀化市| 汝城县| 新安县| 当涂县|