剑指 Offer 59 - II. 队列的最大值
作者:互联网
package leetcode; import java.util.LinkedList; import java.util.Queue; public class offer_59_2 { Queue<Integer> que1; LinkedList<Integer> que2; public offer_59_2() { que1=new LinkedList<Integer>(); que2=new LinkedList<Integer>(); } //双向链表中的头结点是最大值 public int max_value() { if(que2.isEmpty()) { return -1; } return que2.peek(); } public void push_back(int value) { que1.add(value); //维持一个递减序列 while(!que2.isEmpty()&&que2.peekLast()<=value) { que2.pollLast(); } que2.add(value); } public int pop_front() { if(que1.isEmpty()) { return -1; } int temp=que1.poll(); if(!que2.isEmpty()&&temp==que2.peek()) { que2.poll(); } return temp; } }
标签:que1,59,LinkedList,Offer,value,II,que2,public 来源: https://www.cnblogs.com/Yshun/p/16080513.html