Stack — 20181121
12. Min Stack
public class MinStack {
Stack<Integer> stack;
Stack<Integer> minStack; public MinStack() {
// do intialization if necessary
stack = new Stack<>();
minStack = new Stack<>();
} /*
* @param number: An integer
* @return: nothing
*/
public void push(int number) {
// write your code here
stack.push(number);
if (minStack.isEmpty()) {
minStack.push(number);
} else {
minStack.push(Math.min(minStack.peek(), number));
}
} /*
* @return: An integer
*/
public int pop() {
// write your code here
minStack.pop();
return stack.pop();
} /*
* @return: An integer
*/
public int min() {
// write your code here
return minStack.peek();
}
}
575. Decode String
public class Solution {
/**
* @param s: an expression includes numbers, letters and brackets
* @return: a string
*/
public String expressionExpand(String s) {
// write your code here
if (s == null || s.length() == 0) {
return s;
}
String res = "";
Stack<String> stack = new Stack<>();
for (int i = 0; i < s.length(); i++) {
stack.push(s.substring(i, i + 1));
if (stack.peek().equals("]")) {
stack.pop();
String str = "";
String totalStr = "";
while (!stack.isEmpty() && !stack.peek().equals("[")) {
str = stack.pop() + str;
}
stack.pop();
int base = 1;
int num = 0;
while (!stack.isEmpty() && getNum(stack.peek()) >= 0) {
num += base * (getNum(stack.pop()));
base *= 10;
}
for (int j = 0; j < num; j++) {
totalStr += str;
}
if(!totalStr.equals("")){
stack.push(totalStr);
}
}
}
while (!stack.isEmpty()) {
res = stack.pop() + res;
}
return res;
} public int getNum(String s) {
char c = s.charAt(0);
if (c < '0' || c > '9') {
return -1;
}
return c - '0';
}
}
单调栈
122. Largest Rectangle in Histogram
public class Solution {
/**
* @param height: A list of integer
* @return: The area of largest rectangle in the histogram
*/
public int largestRectangleArea(int[] height) {
// write your code here
if (height == null || height.length == 0) {
return 0;
}
Stack<Integer> stack = new Stack<>();
int res = 0;
for (int i = 0; i <= height.length; i++) {
int cur = i == height.length ? -1 : height[i];
if (stack.isEmpty()) {
stack.push(i);
continue;
} while (!stack.isEmpty() && cur <= height[stack.peek()]) {
int h = height[stack.pop()];
int w = stack.isEmpty() ? i : i - stack.peek() -1;
res = Math.max(res, h * w);
}
stack.push(i);
}
return res;
}
}
510. Maximal Rectangle
public class Solution {
/**
* @param matrix: a boolean 2D matrix
* @return: an integer
*/
public int maximalRectangle(boolean[][] matrix) {
// write your code here
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return 0;
}
int r = matrix.length;
int c = matrix[0].length; int[][] height = new int[r][c + 1];
for (int i = 0; i < r; i++) {
for (int j = 0; j <= c; j++) {
if (j == c) {
height[i][j] = -1;
continue;
}
if (i == 0) {
height[i][j] = matrix[i][j] ? 1 : 0;
continue;
}
height[i][j] = !matrix[i][j] ? 0 : height[i - 1][j] + 1;
}
}
int res = 0;
for (int i = 0; i < height.length; i++) {
res = Math.max(res, getMaxRecTangle(height[i]));
}
return res;
} public int getMaxRecTangle(int[] height) {
if (height == null || height.length == 0) {
return 0;
} Stack<Integer> stack = new Stack<>();
int res = 0;
for (int i = 0; i < height.length; i++) {
if (stack.isEmpty()) {
stack.push(i);
continue;
}
while (!stack.isEmpty() && height[i] <= height[stack.peek()]) {
int h = height[stack.pop()];
int w = stack.isEmpty() ? i : i - stack.peek() - 1;
res = Math.max(res, h * w);
}
stack.push(i);
}
return res;
}
}
126. Max Tree
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/ public class Solution {
/**
* @param A: Given an integer array with no duplicates.
* @return: The root of max tree.
*/
public TreeNode maxTree(int[] A) {
// write your code here
TreeNode root = null;
if (A == null || A.length == 0) {
return root;
}
Stack<TreeNode> stack = new Stack<>();
for (int i = 0; i <= A.length; i++) {
TreeNode right = i == A.length ? new TreeNode(Integer.MAX_VALUE) : new TreeNode(A[i]);
if (stack.isEmpty()) {
stack.push(right);
continue;
}
while (!stack.isEmpty() && right.val > stack.peek().val) {
TreeNode node = stack.pop();
if (stack.isEmpty()) {
right.left = node;
break;
}
TreeNode left = stack.peek();
if (left.val < right.val) {
left.right = node;
} else {
right.left = node;
}
}
stack.push(right);
}
return stack.peek().left;
}
}
Stack — 20181121的更多相关文章
- 线性数据结构之栈——Stack
Linear data structures linear structures can be thought of as having two ends, whose items are order ...
- Java 堆内存与栈内存异同(Java Heap Memory vs Stack Memory Difference)
--reference Java Heap Memory vs Stack Memory Difference 在数据结构中,堆和栈可以说是两种最基础的数据结构,而Java中的栈内存空间和堆内存空间有 ...
- [数据结构]——链表(list)、队列(queue)和栈(stack)
在前面几篇博文中曾经提到链表(list).队列(queue)和(stack),为了更加系统化,这里统一介绍着三种数据结构及相应实现. 1)链表 首先回想一下基本的数据类型,当需要存储多个相同类型的数据 ...
- Stack Overflow 排错翻译 - Closing AlertDialog.Builder in Android -Android环境中关闭AlertDialog.Builder
Stack Overflow 排错翻译 - Closing AlertDialog.Builder in Android -Android环境中关闭AlertDialog.Builder 转自:ht ...
- Uncaught RangeError: Maximum call stack size exceeded 调试日记
异常处理汇总-前端系列 http://www.cnblogs.com/dunitian/p/4523015.html 开发道路上不是解决问题最重要,而是解决问题的过程,这个过程我们称之为~~~调试 记 ...
- Stack操作,栈的操作。
栈是先进后出,后进先出的操作. 有点类似浏览器返回上一页的操作, public class Stack<E>extends Vector<E> 是vector的子类. 常用方法 ...
- [LeetCode] Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- [LeetCode] Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- Stack的三种含义
作者: 阮一峰 日期: 2013年11月29日 学习编程的时候,经常会看到stack这个词,它的中文名字叫做"栈". 理解这个概念,对于理解程序的运行至关重要.容易混淆的是,这个词 ...
随机推荐
- [GO]使用go语言实现比特币的工作量证明
之前的博文已经实现了区块连的基本的工作原理,但在比特币系统中有一个很重要的概念:工作量证明POW,在比特币系统中它的作用就是在十分钟左右的时间内只有一个有能够记帐并得到奖励 在之前的博文中,区块的哈希 ...
- 部分类Partial
Partial告诉编译器,一个类,结构,接口的定义源代码可能要分散到一个或者多个源文件中. 在下面的情况下用Partial类型: (1) 类型特别大,不宜放在一个文件中实现.(2) 一个类型中的一部分 ...
- vSphere5.5体系结构
1.vSphere5.5体系结构 vSphere5.5作为云集算操作系统,可以虚拟化服务器.存储.网络在内的整个IT基础架构,将这些架构转化为易于管理的虚拟化!vSphere5.5从逻辑上,可以划分为 ...
- Generated by NetworkManager、ubuntu DNS设置丢失(network-manager造成的情况)
方法一:去掉重启 方法二:卸载network-manager 实测网络不稳,经常掉线(kalinux2.0环境)
- .NET基础 (05)内存管理和垃圾回收
内存管理和垃圾回收1 简述.NET中堆栈和堆的特点和差异2 执行string abc="aaa"+"bbb"+"ccc"共分配了多少内存3 ...
- Maven整理笔记の安装及配置
第一部分:在Windows上安装Maven 检查JDK的安装 在安装Maven之前,首先确认你已经正确安装了JDK.Maven可以运行在JDK1.4及以上版本.先打开Windows命令,运行 ...
- .net 程序集的加载与反射
一. 程序集的加载: 在CLR内部使用System.Reflection.Assembly类的静态LoadFrom方法尝试加载程序集. LoadFrom方法在内部调用Assembly的Load方法,将 ...
- css flex cheat sheet
.container{ display: -webkit-flex/inline-flex; display: -moz-flex/inline-flex; display: -ms-flex/inl ...
- duilib入门简明教程 -- 界面设计器 DuiDesigner (10)
上一个教程讲解了怎么布局最大化.最小化.关闭按钮,但是如果手动去计算这三个按钮的位置和大小的话,非常的不直观,也很不方便. 所以这一章准备介绍duilib的UI设计器,由于这个设计器很不 ...
- c++ inline使函数实现可以在头文件中,避免多重定义错误
作者:Jon Lee链接:https://www.zhihu.com/question/53082910/answer/133612920来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业 ...