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

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

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

2019-11-14 11:53:21
字體:
來源:轉載
供稿:網友

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(); }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 万荣县| 南平市| 西青区| 福建省| 清徐县| 汕尾市| 丽江市| 亚东县| 金乡县| 鹤峰县| 黑河市| 湛江市| 彭水| 新沂市| 永平县| 巴林左旗| 博罗县| 威远县| 会宁县| 聂拉木县| 海淀区| 荔浦县| 巫山县| 安阳市| 襄汾县| 赤峰市| 澄城县| 安泽县| 罗定市| 堆龙德庆县| 昌江| 海兴县| 岳阳县| 克什克腾旗| 应城市| 绿春县| 余庆县| 黎川县| 大厂| 宣恩县| 金塔县|