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

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

不得已二 Java兩個(gè)棧模擬一個(gè)隊(duì)列

2019-11-14 10:30:37
字體:
供稿:網(wǎng)友

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

很簡(jiǎn)單只要記住add操作不需要倒棧 poll 和peek的時(shí)候判斷性的倒棧 肯定能設(shè)計(jì)出來

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(); }}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 桓台县| 睢宁县| 化德县| 汶上县| 扶风县| 兴安盟| 年辖:市辖区| 阿勒泰市| 海原县| 德江县| 东宁县| 松溪县| 宾阳县| 象州县| 大埔县| 三门县| 桑植县| 平利县| 会同县| 赤峰市| 和田市| 鄂伦春自治旗| 阳江市| 涞水县| 贵港市| 临汾市| 上饶县| 察雅县| 临清市| 会昌县| 荆门市| 岳西县| 永修县| 广灵县| 广宗县| 青铜峡市| 玉树县| 常山县| 南召县| 禄丰县| 吐鲁番市|