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. [Training Video - 6] [File Reading] [Java] Create and Write Excel File Using Apache POI API

    package com.file.properties; import java.io.File; import java.io.FileNotFoundException; import java. ...

  2. JDK1.6 1.7 1.8 多版本windows安装 执行命令java -version 版本不变的问题

    现在Windows的java安装已经没有解压版本,Oracle官方也不会再提供了,只有安装程序 所以每当安装一次JDK,都会将 java.exe.javaw.exe.javaws.exe三个可执行文件 ...

  3. 第一次用FontLad~

    FontLad 是一款制作字体的工具 使用流程: 1. 下载.安装软件FontLab Studio2.   File->New菜单,创建一个工程文件,会自动创建一个ASCII码表对应的字体表3. ...

  4. http post 方法传递参数的2种方式

       1.StringEntity try{ HttpPost httpPost = new HttpPost(url); //param参数,可以为param="key1=value1&a ...

  5. 实践作业3:白盒测试----总结与反思DAY12.

    ---恢复内容开始--- 阶段一:熟悉白盒测试方法 负责人:刘思佳 工作质量评价:用例设计详细,考虑到白盒测试基于代码,所以尽可能地覆盖更多的白盒测试方法,对系统可能存在的缺陷就更容易了解.对管理员和 ...

  6. mongodb ---- findAndModify 写法

    db.coll.findAndModify({ query:{x:"ggg"}, update:{$set:{"x":"gggg"}}, f ...

  7. [operator]ELK6 index pattern的问题

    完成了EL/FK的搭建之后,在kibana的主页只能看到默认的索引? 其实这个索引名字的设置是在logstash-smaple.conf(elk6.4)里的设置,比如我这样设置 input { bea ...

  8. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'supplierAction': Injection of resource dependencies failed; nested exception is org.springframework.beans.factor

    这个错误是因为抽象Action类的时候,把ServletContext写成了serverContext,导致无法注入,正确写法是 import javax.annotation.Resource; i ...

  9. ssh的配置[待写]

    开机自启:/etc/rc.local /etc/init.d/ssh start 将 /etc/ssh/sshd_confg中PermitRootLogin no 改为yes,重新启动ssh服务.

  10. hibernate:对于java.lang.NoSuchMethodError: antlr.collections.AST.getLine()I错误解决办法

    在J2EE框架下开发web网站,这种问题经常遇到,只要我们网上搜一下,就可以看到很多版本的,我整理一下:  第一种可能性解决:看看我的项目:主要 是里面的Structs 1.3 (structs 2) ...