12. Min Stack
1 public class MinStack { 2 Stack<Integer> stack; 3 Stack<Integer> minStack; 4 5 public MinStack() { 6 // do intialization if necessary 7 stack = new Stack<>(); 8 minStack = new Stack<>(); 9 } 10 11 /* 12 * @param number: An integer 13 * @return: nothing 14 */ 15 public void push(int number) { 16 // write your code here 17 stack.push(number); 18 if (minStack.isEmpty()) { 19 minStack.push(number); 20 } else { 21 minStack.push(Math.min(minStack.peek(), number)); 22 } 23 } 24 25 /* 26 * @return: An integer 27 */ 28 public int pop() { 29 // write your code here 30 minStack.pop(); 31 return stack.pop(); 32 } 33 34 /* 35 * @return: An integer 36 */ 37 public int min() { 38 // write your code here 39 return minStack.peek(); 40 } 41 }
575. Decode String
1 public class Solution { 2 /** 3 * @param s: an expression includes numbers, letters and brackets 4 * @return: a string 5 */ 6 public String expressionExpand(String s) { 7 // write your code here 8 if (s == null || s.length() == 0) { 9 return s; 10 } 11 String res = ""; 12 Stack<String> stack = new Stack<>(); 13 for (int i = 0; i < s.length(); i++) { 14 stack.push(s.substring(i, i + 1)); 15 if (stack.peek().equals("]")) { 16 stack.pop(); 17 String str = ""; 18 String totalStr = ""; 19 while (!stack.isEmpty() && !stack.peek().equals("[")) { 20 str = stack.pop() + str; 21 } 22 stack.pop(); 23 int base = 1; 24 int num = 0; 25 while (!stack.isEmpty() && getNum(stack.peek()) >= 0) { 26 num += base * (getNum(stack.pop())); 27 base *= 10; 28 } 29 for (int j = 0; j < num; j++) { 30 totalStr += str; 31 } 32 if(!totalStr.equals("")){ 33 stack.push(totalStr); 34 } 35 } 36 } 37 while (!stack.isEmpty()) { 38 res = stack.pop() + res; 39 } 40 return res; 41 } 42 43 public int getNum(String s) { 44 char c = s.charAt(0); 45 if (c < '0' || c > '9') { 46 return -1; 47 } 48 return c - '0'; 49 } 50 }
单调栈
122. Largest Rectangle in Histogram
1 public class Solution { 2 /** 3 * @param height: A list of integer 4 * @return: The area of largest rectangle in the histogram 5 */ 6 public int largestRectangleArea(int[] height) { 7 // write your code here 8 if (height == null || height.length == 0) { 9 return 0; 10 } 11 Stack<Integer> stack = new Stack<>(); 12 int res = 0; 13 for (int i = 0; i <= height.length; i++) { 14 int cur = i == height.length ? -1 : height[i]; 15 if (stack.isEmpty()) { 16 stack.push(i); 17 continue; 18 } 19 20 while (!stack.isEmpty() && cur <= height[stack.peek()]) { 21 int h = height[stack.pop()]; 22 int w = stack.isEmpty() ? i : i - stack.peek() -1; 23 res = Math.max(res, h * w); 24 } 25 stack.push(i); 26 } 27 return res; 28 } 29 }
510. Maximal Rectangle
1 public class Solution { 2 /** 3 * @param matrix: a boolean 2D matrix 4 * @return: an integer 5 */ 6 public int maximalRectangle(boolean[][] matrix) { 7 // write your code here 8 if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { 9 return 0; 10 } 11 int r = matrix.length; 12 int c = matrix[0].length; 13 14 int[][] height = new int[r][c + 1]; 15 for (int i = 0; i < r; i++) { 16 for (int j = 0; j <= c; j++) { 17 if (j == c) { 18 height[i][j] = -1; 19 continue; 20 } 21 if (i == 0) { 22 height[i][j] = matrix[i][j] ? 1 : 0; 23 continue; 24 } 25 height[i][j] = !matrix[i][j] ? 0 : height[i - 1][j] + 1; 26 } 27 } 28 int res = 0; 29 for (int i = 0; i < height.length; i++) { 30 res = Math.max(res, getMaxRecTangle(height[i])); 31 } 32 return res; 33 } 34 35 public int getMaxRecTangle(int[] height) { 36 if (height == null || height.length == 0) { 37 return 0; 38 } 39 40 Stack<Integer> stack = new Stack<>(); 41 int res = 0; 42 for (int i = 0; i < height.length; i++) { 43 if (stack.isEmpty()) { 44 stack.push(i); 45 continue; 46 } 47 while (!stack.isEmpty() && height[i] <= height[stack.peek()]) { 48 int h = height[stack.pop()]; 49 int w = stack.isEmpty() ? i : i - stack.peek() - 1; 50 res = Math.max(res, h * w); 51 } 52 stack.push(i); 53 } 54 return res; 55 } 56 }
126. Max Tree
1 /** 2 * Definition of TreeNode: 3 * public class TreeNode { 4 * public int val; 5 * public TreeNode left, right; 6 * public TreeNode(int val) { 7 * this.val = val; 8 * this.left = this.right = null; 9 * } 10 * } 11 */ 12 13 public class Solution { 14 /** 15 * @param A: Given an integer array with no duplicates. 16 * @return: The root of max tree. 17 */ 18 public TreeNode maxTree(int[] A) { 19 // write your code here 20 TreeNode root = null; 21 if (A == null || A.length == 0) { 22 return root; 23 } 24 Stack<TreeNode> stack = new Stack<>(); 25 for (int i = 0; i <= A.length; i++) { 26 TreeNode right = i == A.length ? new TreeNode(Integer.MAX_VALUE) : new TreeNode(A[i]); 27 if (stack.isEmpty()) { 28 stack.push(right); 29 continue; 30 } 31 while (!stack.isEmpty() && right.val > stack.peek().val) { 32 TreeNode node = stack.pop(); 33 if (stack.isEmpty()) { 34 right.left = node; 35 break; 36 } 37 TreeNode left = stack.peek(); 38 if (left.val < right.val) { 39 left.right = node; 40 } else { 41 right.left = node; 42 } 43 } 44 stack.push(right); 45 } 46 return stack.peek().left; 47 } 48 }