/** * Your CQueue object will be instantiated and called as such: * CQueue* obj = new CQueue(); * obj->appendTail(value); * int param_2 = obj->deleteHead(); */
// 执行用时: 364 ms // 内存消耗: 101.2 MB classCQueue { public: stack<int> stkTail; stack<int> stkHead; CQueue() { } voidappendTail(int value){ stkTail.push(value); } intdeleteHead(){ if (stkHead.empty()) { if (stkTail.empty()) return-1; for (; !stkTail.empty(); stkTail.pop()) stkHead.push(stkTail.top()); } int res = stkHead.top(); stkHead.pop(); return res; } }; /** * Your CQueue object will be instantiated and called as such: * CQueue* obj = new CQueue(); * obj->appendTail(value); * int param_2 = obj->deleteHead(); */
// 执行用时: 53 ms // 内存消耗: 47.3 MB classCQueue{ Deque<Integer> stk1 = new LinkedList<>(); Deque<Integer> stk2 = new LinkedList<>();
publicCQueue(){
} publicvoidappendTail(int value){ stk2.addLast(value); } publicintdeleteHead(){ if (stk1.size() == 0) { if (stk2.size() == 0) return -1; for (; stk2.size() != 0; ) stk1.addLast(stk2.removeLast()); } return stk1.removeLast(); } } /** * Your CQueue object will be instantiated and called as such: * CQueue obj = new CQueue(); * obj.appendTail(value); * int param_2 = obj.deleteHead(); */