国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 宁都县| 潞西市| 右玉县| 界首市| 平舆县| 七台河市| 宁陕县| 清远市| 湛江市| 乐清市| 新余市| 集安市| 久治县| 镇安县| 原平市| 孝义市| 静海县| 昌宁县| 田林县| 佛坪县| 新绛县| 特克斯县| 石屏县| 治县。| 清河县| 荔波县| 柘城县| 当阳市| 桂阳县| 高碑店市| 云林县| 前郭尔| 贞丰县| 巧家县| 如东县| 申扎县| 江口县| 彭泽县| 金华市| 乐亭县| 探索|