牛客网-剑指offer-20-包含min函数的栈
2017-09-14 00:37
337 查看
时间限制: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();
}
}
本题知识点: 栈
算法知识视频讲解
题目描述
定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的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();
}
}
相关文章推荐
- 剑指offer-20.包含min函数的栈
- 剑指Offer_20_包含min函数的栈
- 剑指offer 20 包含min函数的栈
- 剑指offer——20.包含min函数的栈
- 剑指offer_20_包含min函数的栈
- 牛客网剑指offer-包含min函数的栈
- 剑指Offer面试题20顺时针打印矩阵,面试题21包含min函数的栈
- 【剑指offer】20包含min函数的栈
- (C++)剑指offer-20:包含min函数的栈(举例让抽象具体化)
- [剑指offer][面试题21]包含min函数的栈
- 剑指offer-面试题21.包含min函数的栈
- 【剑指offer-Java版】21包含min函数的栈
- 剑指offer---包含min函数的桟
- 【剑指offer】实现包含min函数的栈
- 剑指offer 面试题21:包含min函数的栈
- 剑指Offer----包含min函数的栈
- 【剑指offer】面试题21:包含min函数的栈
- 包含min函数的栈 (剑指Offer 第 19 题)
- 剑指Offer面试题:19.包含Min函数的栈
- 剑指Offer - 九度1522 - 包含min函数的栈