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. NAT穿透的详细讲解及分析.RP

    原创出处:https://bbs.pediy.com/thread-131961.htm 转载来源: https://blog.csdn.net/g_brightboy/article/details ...

  2. AlwaysOn的数据同步原理

    摘抄自<SQL Server 2012实施与管理实战指南> 镜像的工作原理: 那么主体数据库和镜像数据库是如何同步数据的呢?SQL数据库中任何的数据变化都会先记录到事务日志中,然后才会真正 ...

  3. [GO]并行和并发的区别

    并行:指在同一时刻,有多条指令在多个处理器上同时执行 并发:指在同一时刻只能有一条指令执行,但多个进程指令被快速的轮换执行,使得在宏观上具有多个进程同时执行的效果,但在微观上并不是同时执行的,只有把时 ...

  4. [GO]方法的重写

    package main import "fmt" type Person struct { name string sex byte age int } func (tmp Pe ...

  5. java IO类简单介绍

    一.流的概念 流是字节序列的抽象概念.流和文件的差别:文件是数据的静态存储形式,而流是指数据传输时的形态.文件只是流的操作对象之一.流按其操作的对象不同可以分为文件流.网络流.内存流.磁带流等.Jav ...

  6. Linux下oracle定时备份

    1. 设置数据库空表可导出(oracel11g) 用PL/SQL登录数据库(或者其他工具) 执行: select 'alter table '||table_name||' allocate exte ...

  7. 使用Word2016发布CSDN博客

    目前大部分的博客作者在用Word写博客这件事情上都会遇到以下3个痛点: 1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.使用Word写 ...

  8. How do I avoid capturing self in blocks when implementing an API?

    Short answer Instead of accessing self directly, you should access it indirectly, from a reference t ...

  9. MATLAB搬移到别的电脑出现License Manager Error -9

    是注册码的问题,不需要重装,主要是以前的安装包不见了.解决办法: 下一个KeyGen的MLMCrypt.exe文件.运行之后在当前目录下出现一个LICENSE.DAT文件. 复制到matlab.exe ...

  10. Hibernate Validator bean-validator-3.0-JBoss-4.0.2

    信息: Hibernate Validator bean-validator-3.0-JBoss-4.0.2 后面是一大段错误信息不贴出来了... 解决方案:hibernate配置文件中加入 < ...