当前位置:网站首页>Sword finger offer 09 Implementing queues with two stacks
Sword finger offer 09 Implementing queues with two stacks
2022-07-05 08:21:00 【Programmer Xiao Li】
Use two stacks to implement a queue . The declaration of the queue is as follows , Please implement its two functions appendTail and deleteHead , The functions of inserting integers at the end of the queue and deleting integers at the head of the queue are respectively completed .( If there are no elements in the queue ,deleteHead Operation return -1 )
Example 1:
Input :
["CQueue","appendTail","deleteHead","deleteHead"]
[[],[3],[],[]]
Output :[null,null,3,-1]
Example 2:
Input :
["CQueue","deleteHead","appendTail","appendTail","deleteHead","deleteHead"]
[[],[],[5],[2],[],[]]
Output :[null,-1,null,null,5,2]
Tips :
1 <= values <= 10000
At most appendTail、deleteHead Conduct 10000 Secondary call
Ideas :
1. An insert stack , An output stack . When inserting data , Write into the insert stack .
2. When outputting data , Pop up from the output stack first , If the output stack does not exist , Then press... From the input stack in turn .
class CQueue {
Stack<Integer> pushStack = new Stack<Integer>();
Stack<Integer> popStack = new Stack<Integer>();
public CQueue() {
}
public void appendTail(int value) {
pushStack.push(value);
}
public int deleteHead() {
if (popStack.empty()){
while (!pushStack.empty()){
popStack.push(pushStack.pop());
}
}
if (popStack.empty()){
return -1;
}
return popStack.pop();
}
}
/**
* Your CQueue object will be instantiated and called as such:
* CQueue obj = new CQueue();
* obj.appendTail(value);
* int param_2 = obj.deleteHead();
*/
边栏推荐
- Live555 push RTSP audio and video stream summary (III) flower screen problem caused by pushing H264 real-time stream
- Carrier period, electrical speed, carrier period variation
- What are the test items of power battery ul2580
- 动力电池UL2580测试项目包括哪些
- Void* C is a carrier for realizing polymorphism
- Negative pressure generation of buck-boost circuit
- Shape template matching based on Halcon learning [VII] reuse_ model. Hdev routine
- General makefile (I) single C language compilation template
- C, Numerical Recipes in C, solution of linear algebraic equations, LU decomposition source program
- Explain task scheduling based on Cortex-M3 in detail (Part 2)
猜你喜欢
Circleq of linked list
[tutorial 19 of trio basic from introduction to proficiency] detailed introduction of trio as a slave station connecting to the third-party bus (anybus PROFIBUS DP...)
C language # and #
PMSM dead time compensation
STM32 single chip microcomputer -- volatile keyword
Class of color image processing based on Halcon learning_ ndim_ norm. hdev
实例003:完全平方数 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
Relationship between line voltage and phase voltage, line current and phase current
OC and OD gate circuit
MySQL MHA high availability cluster
随机推荐
动力电池UL2580测试项目包括哪些
Sizeof (function name) =?
My-basic application 2: my-basic installation and operation
Shape template matching based on Halcon learning [viii] PM_ multiple_ models. Hdev routine
STM32 single chip microcomputer - bit band operation
Measurement fitting based on Halcon learning [i] fuse Hdev routine
C WinForm [change the position of the form after running] - Practical Exercise 4
Measurement fitting based on Halcon learning [III] PM_ measure_ board. Hdev routine
Stm32--- systick timer
Tailq of linked list
Briefly talk about the identification protocol of mobile port -bc1.2
Management and use of DokuWiki
Use indent to format code
Take you to understand the working principle of lithium battery protection board
实例003:完全平方数 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
C WinForm [help interface - send email] - practice five
Process communication mode between different hosts -- socket
Network communication process
Soem EtherCAT source code analysis II (list of known configuration information)
Various types of questions judged by prime numbers within 100 (C language)