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的更多相关文章

  1. 线性数据结构之栈——Stack

    Linear data structures linear structures can be thought of as having two ends, whose items are order ...

  2. Java 堆内存与栈内存异同(Java Heap Memory vs Stack Memory Difference)

    --reference Java Heap Memory vs Stack Memory Difference 在数据结构中,堆和栈可以说是两种最基础的数据结构,而Java中的栈内存空间和堆内存空间有 ...

  3. [数据结构]——链表(list)、队列(queue)和栈(stack)

    在前面几篇博文中曾经提到链表(list).队列(queue)和(stack),为了更加系统化,这里统一介绍着三种数据结构及相应实现. 1)链表 首先回想一下基本的数据类型,当需要存储多个相同类型的数据 ...

  4. Stack Overflow 排错翻译 - Closing AlertDialog.Builder in Android -Android环境中关闭AlertDialog.Builder

    Stack Overflow 排错翻译  - Closing AlertDialog.Builder in Android -Android环境中关闭AlertDialog.Builder 转自:ht ...

  5. Uncaught RangeError: Maximum call stack size exceeded 调试日记

    异常处理汇总-前端系列 http://www.cnblogs.com/dunitian/p/4523015.html 开发道路上不是解决问题最重要,而是解决问题的过程,这个过程我们称之为~~~调试 记 ...

  6. Stack操作,栈的操作。

    栈是先进后出,后进先出的操作. 有点类似浏览器返回上一页的操作, public class Stack<E>extends Vector<E> 是vector的子类. 常用方法 ...

  7. [LeetCode] Implement Stack using Queues 用队列来实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  8. [LeetCode] Min Stack 最小栈

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  9. Stack的三种含义

    作者: 阮一峰 日期: 2013年11月29日 学习编程的时候,经常会看到stack这个词,它的中文名字叫做"栈". 理解这个概念,对于理解程序的运行至关重要.容易混淆的是,这个词 ...

随机推荐

  1. PM2部署资料

    问题1:pm2的log怎么查看?(pm2 show (name))可以看到日志地址,直接查看 问题2:日志怎么清除:pm2 flush 问题3:修改日志输出路径 问题4:怎么重新加载,restart貌 ...

  2. linux常用Java程序员使用命令(一)

    pwd 显示当前路径cd 切换目录 . .. ~ls 显示文件(夹) -l 显示详细信息 -a 显示全部,包括隐藏文件(夹) mkdir 创建文件夹 -p 递归创建 touch 创建空白文件 echo ...

  3. java Calender类

    1.Calender和Date相互转化 public static void main(String[] args) { // TODO Auto-generated method stub Cale ...

  4. 自己(转)JAVA中toString方法的作用

    JAVA中toString方法的作用 因为它是Object里面已经有了的方法,而所有类都是继承Object,所以“所有对象都有这个方法”. 它通常只是为了方便输出,比如System.out.print ...

  5. Lotus迁移到Exchange 2010 POC 之Domino Server的配置!

    1.      在桌面点击安装完成的Domino 服务器配置:

  6. CentOS 7.2安装zabbix 3.0 LTS

    1.zabbix简介 zabbix(音同 zæbix)是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供 ...

  7. python中用ElementTree.iterparse()读取xml文件中的多层节点

    我在使用Python解析比较大型的xml文件时,为了提高效率,决定使用iterparse()方法,但是发现根据网上的例子:每次if event == 'end':之后elem.clear()或者是每次 ...

  8. 自定义spring valid方式实现验证

    推荐:http://blog.csdn.net/xulianboblog/article/details/51694924

  9. 获取vmware虚拟机模板

    在我们通过克隆虚机,需要用到虚机模板.在数据中心的目录下面有文件夹.模板和虚拟机,那么这里需要做的是根据类型做递归查询. private void GetTemplate() { System.Tex ...

  10. Xamarin.Forms中 Navigation,NavigationPage详解

    1.Xamarin Forms下有四个成员类:Element,VisualElement,Page,NavigationPage 基类为Element,继承的子类分别是VisualElement,Pa ...