当前位置:网站首页>One brush 314 sword finger offer 09 Implement queue (E) with two stacks
One brush 314 sword finger offer 09 Implement queue (E) with two stacks
2022-04-23 15:40:00 【Tang and Song Dynasties】
subject :
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 :
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 :
Their thinking :
The stack cannot implement the queue function : Stack bottom element ( Corresponding to the first element of the team ) Can't delete directly , All the elements above need to be out of the stack .
Double stack can realize the reverse order of the list : There is a stack with three elements A = [1,2,3] Empty stack B = []
If the loop executes A Element out of the stack and added into the stack B , Until the stack A It's empty , be A = []B = [3,2,1],
namely Stack B Element implementation stack A Elements in reverse order .
Utilization stack B Delete team leader element : In reverse order ,B Executing out of the stack is equivalent to deleting A At the bottom of the stack , That is, the corresponding team head element .
Function design :
The title only requires the realization of To join a party appendTail() and Delete team leader deleteHead() The normal operation of the two functions ,
So we can design the stack A Used to join the end of the queue , Stack B Used to reverse the order of elements , So as to delete the team head element .
To join a party appendTail() function : The digital val Join the stack A that will do .
Delete team leader deleteHead() function : There are three situations .
Stack B Not empty : B There are still elements in reverse order , So go straight back B Top element of .
otherwise , When A It's empty : That is, both stacks are empty , Element free , Therefore return -1.
otherwise : Stack A All elements are transferred to the stack B in , Implement elements in reverse order , And return to the stack B Top element of .
-----------
Complexity analysis :
Because of the special problem , The following analysis only meets the requirements of adding N And delete N Elements , That is, the stack is empty in the initial and end states .
Time complexity : appendTail() Function is O(1);deleteHead()
Function in N A total of... Needs to be completed in the deletion of the first element of the team N The reverse order of the elements .
Spatial complexity O(N): In the worst case , Stack A and B Keep together N Elements .
---------------------
class CQueue {
//A Used to add B Used to delete B yes A In reverse order
LinkedList<Integer> A, B;// Define two stacks
public CQueue() {
// initialization
A = new LinkedList<Integer>();
B = new LinkedList<Integer>();
}
public void appendTail(int value) {
//A Add an element at the end of the
A.addLast(value);
}
public int deleteHead() {
if (!B.isEmpty()) return B.removeLast();// Special judgement
if (A.isEmpty()) return -1;// Special judgement
while (!A.isEmpty()) {
//B It's empty A Not empty , First, let B get A The elements of
B.addLast(A.removeLast());
}
return B.removeLast();
}
}
notes : return B.removeLast(); To return as a value
/** * Your CQueue object will be instantiated and called as such: * CQueue obj = new CQueue(); * obj.appendTail(value); * int param_2 = obj.deleteHead(); */
版权声明
本文为[Tang and Song Dynasties]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231535203756.html
边栏推荐
- Single architecture system re architecture
- What if the package cannot be found
- After time judgment of date
- Mobile finance (for personal use)
- 基于 TiDB 的 Apache APISIX 高可用配置中心的最佳实践
- What are the mobile app software testing tools? Sharing of third-party software evaluation
- Introduction to dynamic programming of leetcode learning plan day3 (198213740)
- el-tree实现只显示某一级复选框且单选
- 软件性能测试报告起着什么作用?第三方测试报告如何收费?
- mysql乐观锁解决并发冲突
猜你喜欢
随机推荐
控制结构(一)
Functions (Part I)
Mumu, go all the way
The El tree implementation only displays a certain level of check boxes and selects radio
编译,连接 -- 笔记
Multi level cache usage
通过 PDO ODBC 将 PHP 连接到 MSSQL
Crawling fragment of a button style on a website
山寨版归并【上】
深度学习调参的技巧
Independent operation smart farm Innovation Forum
自主作业智慧农场创新论坛
Redis master-slave replication process
MySQL集群模式與應用場景
MySQL集群模式与应用场景
Node.js ODBC连接PostgreSQL
自动化测试框架常见类型▏自动化测试就交给软件测评机构
IronPDF for .NET 2022.4.5455
移动app软件测试工具有哪些?第三方软件测评小编分享
shell脚本中的DATE日期计算