牛客网-剑指offer-20-包含min函数的栈
来源:互联网 发布:一年java专业技能描述 编辑:程序博客网 时间:2024/05/17 07:56
时间限制:1秒 空间限制:32768K 热度指数:97068
算法知识视频讲解题目描述
定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。
用2个栈
import java.util.Stack;
public class Solution {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
stack1.push(node);
if(stack2.isEmpty()) {
stack2.push(node);
}else {
stack2.push(Math.min(node, stack2.peek()));
}
}
public void pop() {
stack1.pop();
stack2.pop();
}
public int top() {
return stack1.peek();
}
public int min() {
return stack2.peek();
}
}
public class Solution {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
stack1.push(node);
if(stack2.isEmpty()) {
stack2.push(node);
}else {
stack2.push(Math.min(node, stack2.peek()));
}
}
public void pop() {
stack1.pop();
stack2.pop();
}
public int top() {
return stack1.peek();
}
public int min() {
return stack2.peek();
}
}
阅读全文
0 0
- 牛客网-剑指offer-20-包含min函数的栈
- 【剑指offer】包含min函数的栈
- 剑指offer--包含min函数的栈
- 剑指Offer-->包含min函数的栈
- 《剑指offer》包含min函数的栈
- 剑指offer - 包含min函数的栈
- 剑指offer-包含min函数的栈
- 剑指offer:包含min函数的栈
- 包含min函数的栈|剑指offer
- 剑指offer-包含min函数的栈
- 剑指offer 包含min函数的栈
- 剑指offer:包含min函数的栈
- 剑指offer 包含min函数的栈
- [剑指offer]包含min函数的栈
- 《剑指offer》-包含min函数的栈
- 剑指offer----包含min函数的栈
- 【剑指offer】包含min函数的栈
- 剑指offer|包含min函数的栈
- 2017.9.14 dispatching 思考记录
- Oracle字符集GBK改为UTF-8
- HDU
- win32汇编无头文件编译成功
- 程序32
- 牛客网-剑指offer-20-包含min函数的栈
- Ubuntu 下实现FTP服务器,SSH服务器,邮件服务器,Web服务器(一)
- 1006. Sign In and Sign Out (25)
- 程序33
- rem布局在webview中页面错乱
- Synchronized与Lock锁的区别
- bzoj 1681: [Usaco2005 Mar]Checking an Alibi 不在场的证明(BFS)
- Spring 框架简介
- H5中关于自定义属性的设置和获取