当前位置:网站首页>剑指 Offer 第 1 天栈与队列(简单)
剑指 Offer 第 1 天栈与队列(简单)
2022-06-25 12:19:00 【辞树 LingTree】
剑指 Offer 09. 用两个栈实现队列
https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/
import java.util.Stack;
class CQueue {
Stack<Integer> a;
Stack<Integer> b;
public CQueue() {
a = new Stack<Integer>();
b = new Stack<Integer>();
}
public void appendTail(int value) {
a.push(value);
}
public int deleteHead() {
if(b.empty()) {
while(!a.empty()){
b.push(a.pop());
}
}
if(b.empty()){
return -1;
}
else{
return b.pop();
}
}
}
/**
* Your CQueue object will be instantiated and called as such:
* CQueue obj = new CQueue();
* obj.appendTail(value);
* int param_2 = obj.deleteHead();
*/剑指 Offer 30. 包含min函数的栈
https://leetcode-cn.com/problems/bao-han-minhan-shu-de-zhan-lcof/
import java.util.Stack;
public class MinStack {
Stack<Integer> a;
Stack<Integer> b;
/** initialize your data structure here. */
public MinStack() {
a = new Stack<Integer>();
b = new Stack<Integer>();
}
public void push(int x) {
a.push(x);
if(b.empty() || b.peek() >= x) {
b.push(x);
}
}
public void pop() {
int t = a.pop();
if(t == b.peek()) {
int k = b.pop();
}
}
public int top() {
return a.peek();
}
public int min() {
int pos = b.size() - 1;
return b.peek();
}
}边栏推荐
- 2021-09-02
- Koa 框架
- 美创入选“2022 CCIA中国网络安全竞争力50强”榜单
- Jupyter Notebook主题字体设置及自动代码补全
- torch. Tensor splicing and list (tensors)
- 2021-09-30
- AI assisted paper drawing of PPT drawing
- MySQL writes user-defined functions and stored procedure syntax (a detailed case is attached, and the problem has been solved: errors are reported when running user-defined functions, and errors are r
- Concat(), join(), reverse(), sort() method in JS array
- Jenkins Pipeline使用
猜你喜欢

Elemntui's select+tree implements the search function

515. Find Largest Value in Each Tree Row

MySQL adds, modifies, and deletes table fields, field data types, and lengths (with various actual case statements)

Three jobs! You can learn this from me (attached with graduation vlog)

【AI助力科研】loss曲线傻瓜式绘制

Geospatial search - > R tree index

(6) Pyqt5--- > window jump (registration login function)

画图常用配色

CUDA error: unspecified launch failure

laravel 9
随机推荐
The editor is used every day. What is the working principle of language service protocol?
Geospatial search: implementation principle of KD tree
5 kinds of viewer for browser
How to implement a high-performance load balancing architecture?
MySQL and excel tables importing database data (Excel for MySQL)
Guess Tongyuan B
按权重随机选择[前缀和+二分+随机target]
顺序表的折半查找法
(4) Pyqt5 tutorial -- > Custom signal and slot (super winding...)
First acquaintance with CANopen
MySQL writes user-defined functions and stored procedure syntax (a detailed case is attached, and the problem has been solved: errors are reported when running user-defined functions, and errors are r
Elemntui's select+tree implements the search function
Ramda rejects objects with null and empty object values in the data
Initialize the project using the express framework
Shell learning notes (latest update: 2022-02-18)
RESTful和RPC
Concat(), join(), reverse(), sort() method in JS array
[Visio]平行四边形在Word中模糊问题解决
Why are databases cloud native?
用include what you use拯救混乱的头文件