classCQueue{ LinkedList<Integer> A, B; publicCQueue(){ A = new LinkedList<Integer> (); B = new LinkedList<Integer> (); } publicvoidappendTail(int value){ A.addLast(value); } publicintdeleteHead(){ if(!B.isEmpty()) return B.removeLast(); if(A.isEmpty()) return -1; while(!A.isEmpty()){ B.addLast(A.removeLast()); } return B.removeLast(); } }
/** * Your CQueue object will be instantiated and called as such: * CQueue obj = new CQueue(); * obj.appendTail(value); * int param_2 = obj.deleteHead(); */
classMinStack{ Stack<Integer> A, B; /** initialize your data structure here. */ publicMinStack(){ A = new Stack<> (); B = new Stack<> (); } publicvoidpush(int x){ A.push(x); if(B.empty() || x <= B.peek()){ B.push(x); }else{ B.push(B.peek()); } } publicvoidpop(){ A.pop(); B.pop(); } publicinttop(){ return A.peek(); } publicintmin(){ return B.peek(); } }
/** * Your MinStack object will be instantiated and called as such: * MinStack obj = new MinStack(); * obj.push(x); * obj.pop(); * int param_3 = obj.top(); * int param_4 = obj.min(); */