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. 电脑破解wifi密码(至少连过1次的才可以)

    电脑破解wifi密码(至少连过1次的才可以) 连过的wifi密码忘记了怎么办? 只要你电脑连过的都能破解. cmd输入以下内容查看电脑连接过的wifi名字. netsh wlan show profi ...

  2. 工作流系统webservice服务

    http://blog.csdn.net/zhengzhb/article/details/7477616

  3. jdk1.7 环境变量配置

    Windows系统中设置环境变量如下图右击“我的电脑”,选择“属性”. 点击“高级”选项卡,选择“环境变量”.  在“系统环境变量”中设置上面提到的3个环境变量,如果变量已经存在就选择“编辑”,否则选 ...

  4. git 进阶操作

    1.blame git blame +文件名,可以查看到某个文件每一行最近一次是由谁编辑修改的.-L 22,33 选项可以制定 2.bisect 开始git bisect:   $ git bisec ...

  5. 如何查找文件中的schema约束

    1.下载一个spring3.2的jar和source 然后打开source的文件(路径:\spring-framework-3.2.5.RELEASE\docs\spring-framework-re ...

  6. jQuary总结11:jQuery插件封装---jQuery封装 手风琴 动画插件

    完整代码下载点击我的GitHub: https://github.com/XingJYGo/jquery-accordion 1 手风琴的效果展示如下: 2 封装插件目录结构如下: 主要包括:HTML ...

  7. 启动samba服务--ubuntu 14.04

    1. 修改配置文件 /etc/samba/smb.conf文件末尾添加 [homes] comment = Home Directories browseable = yes read only = ...

  8. modelsim使用常见问题及解决办法集锦③

    四.You selected Modelsim-Altera as Simulation Software in EDA Tool Settings,however…… You selected Mo ...

  9. chrome console cheat sheet

    快捷键 打开console界面快捷键:ctrl+shift+J 清空log:ctrl+L 选项 Log XMLHTTPRequests:显示ajax Preserve log upon navigat ...

  10. 27款经典的CSS框架

    利用 CSS 框架,可以简化你的工作,提高工作效率.CSS 框架是一系列 CSS 文件的集合体,包含了基本的元素重置,页面排版.网格布局.表单样式.通用规则等代码块.下面给你推荐了27款优秀的CSS框 ...