数据结构解题

Stack

  1. MinStack

    

  2 Longest Valid Parentheses

  求最长的合理的()

  • 利用stack做(的积累和匹配
  • 利用gobal变量和local变量做更新
  • 注意 ()(()的时候,也就是说 当)与stack里的(做了匹配之后 需要判断stack是否为空, 不为空的话,往后继续累积,往前的话就断掉了!  
  •  ()(()()()() 在黄色空间里,做了匹配,可以往后扩展,但是向前的地方就要看下一个char是否能把stack清空,上面的例子不行 下面这个例子可以  ()(())

  

 1 public class Solution {
 2     public int longestValidParentheses(String s) {
 3         if (s == null || s.length() == 0) {
 4             return 0;
 5         }
 6         if (s == null) {
 7             return 0;
 8         }
 9 
10         Stack<Integer> stack = new Stack<Integer>();
11         int maxLen = 0;
12         int accumulatedLen = 0;
13 
14         for(int i = 0; i < s.length(); i++) {
15             if(s.charAt(i) == '(') {
16                 stack.push(i);
17             } else {
18                 if (stack.isEmpty()) {
19                     accumulatedLen = 0;
20                 } else {
21                     int matchedPos = stack.pop();
22                     int matchedLen = i - matchedPos + 1;
23 
24                     if (stack.isEmpty()) {
25                         accumulatedLen += matchedLen;
26                         matchedLen = accumulatedLen;
27                     } else {
28                         matchedLen = i - stack.peek();
29                     }
30 
31                     maxLen = Math.max(maxLen, matchedLen);
32                 }
33             }
34             maxLen = Math.max(maxLen,accumulatedLen);
35          }
36     return maxLen;
37     }
38 }
View Code

  3. maximal-rectangle

  首先用坐标型动态规划拿到一个高度的数组。

  其次利用stack, 递增时不断的push, 当遇到一个比stack的值小的时候,相当于木桶原理的短板的时候,求这个时候的面积。

  当一直往后走,到达末位的时候,都没有比当前stack低的值的时候,那么就是从当前pos到最后这么长作为宽度。

 1 public class Solution {
 2     public int maximalRectangle(char[][] matrix) {
 3         if (matrix == null || matrix.length == 0) {
 4             return 0;
 5         }
 6         
 7         //准备一个表示高度的matrix
 8         int m = matrix.length;
 9         int n = matrix[0].length;
10         int[][] height = new int[m][n];
11         
12         //坐标类动态规划得到高度矩阵
13         for (int i = 0; i < m; i++) {
14             for (int j = 0; j < n; j++) {
15                 if (i == 0) {
16                     height[i][j] = ((matrix[i][j] == '1')? 1: 0);
17                 } else {
18                     height[i][j] = ((matrix[i][j] == '1') ? height[i - 1][j] + 1 : 0);
19                 }       
20             }
21         }
22       
23       
24        
25         
26         int result = 0;
27         for (int i = 0; i < m; i++) {
28             Stack<Integer> myStack = new Stack<Integer>();
29             for (int j = 0; j < n; j++) {
30                 //当有一个值比当前的stack最外面点低的时候,拿到这段区间的面积
31                 while (!myStack.empty() && height[i][j] < height[i][myStack.peek()]) {
32                     int pos = myStack.peek();
33                     myStack.pop();
34                     int temp = height[i][pos]* (myStack.empty()? j: j - myStack.peek() - 1);
35                     result = Math.max(result, temp);
36                     
37                 }
38                 myStack.push(j);
39             }
40             
41             //当走到最后都没有值比stack里面的点低
42             while (!myStack.empty()) {
43                 int pos = myStack.peek();
44                 myStack.pop();
45                 //需要检测是否为空!
46                 int temp = height[i][pos]* (myStack.empty()? n: n - myStack.peek() - 1);
47                     result = Math.max(result, temp);
48             }
49         }
50         return result; 
51         
52     }
53 }
View Code

 

posted @ 2016-09-14 15:09  毛线刷题笔记  阅读(141)  评论(0编辑  收藏  举报