刷题-Min Stack 缺python
来源:互联网 发布:淘宝小号挂机赚钱 编辑:程序博客网 时间:2024/04/29 03:22
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
- push(x) -- Push element x onto stack.
- pop() -- Removes the element on top of the stack.
- top() -- Get the top element.
- getMin() -- Retrieve the minimum element in the stack.
java:
class MinStack {
Node top = null ;
public void push(int x) {
if (top == null){
top = new Node(x);
top.min = x;
}
else{
Node temp = new Node(x);
temp.next = top;
top = temp;
top.min = Math.min(top.next.min,x);
}
}
public void pop() {
top = top.next;
return;
}
public int top() {
return top == null ? 0 : top.val;
}
public int getMin() {
return top == null ? 0:top.min;
}
}
class Node{
int min;
int val;
Node next;
public Node(int val){
this.val = val;
}
}
Node top = null ;
public void push(int x) {
if (top == null){
top = new Node(x);
top.min = x;
}
else{
Node temp = new Node(x);
temp.next = top;
top = temp;
top.min = Math.min(top.next.min,x);
}
}
public void pop() {
top = top.next;
return;
}
public int top() {
return top == null ? 0 : top.val;
}
public int getMin() {
return top == null ? 0:top.min;
}
}
class Node{
int min;
int val;
Node next;
public Node(int val){
this.val = val;
}
}
0 0
- 刷题-Min Stack 缺python
- LeetCode刷题 (Python) | 155. Min Stack
- Min Stack Leetcode Python
- 155. Min Stack [easy] (Python)
- [leetcode: Python]155.Min Stack
- [刷题]Min Stack
- min stack
- Min Stack
- Min Stack
- Min Stack
- Min Stack
- Min Stack
- Min Stack
- Min Stack
- Min Stack
- Min Stack
- Min Stack
- Min Stack
- jps无法显示jvm进程
- HTML5权威指南读书笔记【第三部分】
- 国内外DNS服务器地址列表
- 如何杀掉D,Z状态的进程
- 删除文件夹里的 .svn
- 刷题-Min Stack 缺python
- 12月1周 1 数组
- win连接不到远程计算机
- ngx事件驱动机制
- jquery jqgrid 学习记录一
- 刷题-Valid Sudoku 缺python
- 前端开发到底需要掌握哪些知识?
- 发布网站后登陆主页只显示Lable字样
- HTTP协议--缓存 (四)