leetcode 20 有效的括号

给定一个只包括 ‘(‘,’)’,’{‘,’}’,’[‘,’]’ 的字符串 s ,判断字符串是否有效。
有效字符串需满足:

  1. 左括号必须用相同类型的右括号闭合。
  2. 左括号必须以正确的顺序闭合。
    ```java //v1.0 利用栈的思想 class Solution { public boolean isValid(String s) {
    1. Map<Character,Character> map = new HashMap<>();
    2. map.put('(',')');
    3. map.put('[',']');
    4. map.put('{','}');
    5. LinkedList<Character> stack = new LinkedList<>();
    6. for(char c:s.toCharArray()){
    7. if(map.containsKey(c)) stack.add(c);
    8. else if(stack.size()==0||c!=map.get(stack.removeLast())) return false;
    9. }
    10. return stack.size()==0;
  1. }

}

  1. <a name="hWqkS"></a>
  2. ####
  3. <a name="ru5X9"></a>
  4. #### 剑指Offer 09 用两个栈实现队列
  5. 用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 appendTail 和 deleteHead ,分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素,deleteHead 操作返回 -1 )
  6. ```java
  7. class CQueue {
  8. private LinkedList<Integer> list1;
  9. private LinkedList<Integer> list2;
  10. public CQueue() {
  11. list1 = new LinkedList<>();
  12. list2 = new LinkedList<>();
  13. }
  14. public void appendTail(int value) {
  15. list1.add(value);
  16. }
  17. public int deleteHead() {
  18. if(list2.size()!=0) return list2.removeLast();
  19. else if(list1.size() == 0) return -1;
  20. else{
  21. while(list1.size()!=0){
  22. list2.add(list1.removeLast());
  23. }
  24. return list2.removeLast();
  25. }
  26. }
  27. }
  28. /**
  29. * Your CQueue object will be instantiated and called as such:
  30. * CQueue obj = new CQueue();
  31. * obj.appendTail(value);
  32. * int param_2 = obj.deleteHead();
  33. */

leetcode 225用队列实现栈

  1. class MyStack {
  2. private LinkedList<Integer> queue1;
  3. private LinkedList<Integer> queue2;
  4. /** Initialize your data structure here. */
  5. public MyStack() {
  6. queue1 = new LinkedList<>();
  7. queue2 = new LinkedList<>();
  8. }
  9. /** Push element x onto stack. */
  10. public void push(int x) {
  11. if(queue1.size()!=0) queue1.add(x);
  12. else if(queue2.size()!=0) queue2.add(x);
  13. else queue1.add(x);
  14. }
  15. /** Removes the element on top of the stack and returns that element. */
  16. public int pop() {
  17. if(queue1.size()==0&&queue2.size()==0) return -1;
  18. else if(queue1.size()!=0){
  19. while(queue1.size()!=1){
  20. queue2.add(queue1.removeFirst());
  21. }
  22. return queue1.removeFirst();
  23. }
  24. else{
  25. while(queue2.size()!=1){
  26. queue1.add(queue2.removeFirst());
  27. }
  28. return queue2.removeFirst();
  29. }
  30. }
  31. /** Get the top element. */
  32. public int top() {
  33. int topNum;
  34. if(queue1.size()==0&&queue2.size()==0) return -1;
  35. else if(queue1.size()!=0){
  36. while(queue1.size()!=1){
  37. queue2.add(queue1.removeFirst());
  38. }
  39. topNum = queue1.getFirst();
  40. queue2.add(queue1.removeFirst());
  41. return topNum;
  42. }
  43. else{
  44. while(queue2.size()!=1){
  45. queue1.add(queue2.removeFirst());
  46. }
  47. topNum = queue2.getFirst();
  48. queue1.add(queue2.removeFirst());
  49. return topNum;
  50. }
  51. }
  52. /** Returns whether the stack is empty. */
  53. public boolean empty() {
  54. if(queue1.isEmpty()&&queue2.isEmpty()) return true;
  55. return false;
  56. }
  57. }
  58. /**
  59. * Your MyStack object will be instantiated and called as such:
  60. * MyStack obj = new MyStack();
  61. * obj.push(x);
  62. * int param_2 = obj.pop();
  63. * int param_3 = obj.top();
  64. * boolean param_4 = obj.empty();
  65. */

leetcode84 柱状图中最大的矩形

可以把这个想象成锯木板,如果木板都是递增的那我很开心,如果突然遇到一块木板i矮了一截,那我就先找之前最戳出来的一块(其实就是第i-1块),计算一下这个木板单独的面积,然后把它锯成次高的,这是因为我之后的计算都再也用不着这块木板本身的高度了。再然后如果发觉次高的仍然比现在这个i木板高,那我继续单独计算这个次高木板的面积(应该是第i-1和i-2块),再把它俩锯短。直到发觉不需要锯就比第i块矮了,那我继续开开心心往右找更高的。当然为了避免到了最后一直都是递增的,所以可以在最后加一块高度为0的木板。

  1. class Solution {
  2. public int largestRectangleArea(int[] heights) {
  3. int len = heights.length;
  4. int[] copy = new int[len+2];
  5. System.arraycopy(heights,0,copy,1,len);
  6. int area = 0;
  7. Stack<Integer> stack = new Stack<>();
  8. for(int i=0;i<len+2;i++){
  9. while(!stack.empty()&&(copy[stack.peek()]>copy[i])){
  10. int index = stack.pop();
  11. area = Math.max(area, copy[index]*(i-stack.peek()-1));
  12. }
  13. stack.push(i);
  14. }
  15. return area;
  16. }
  17. }

leetcode85 最大矩形

给定一个仅包含 0 和 1 、大小为 rows x cols 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积。

  1. class Solution {
  2. int area = 0;
  3. public int maximalRectangle(char[][] matrix) {
  4. int row = matrix.length;
  5. if(row==0){
  6. return 0;
  7. }
  8. int column = matrix[0].length;
  9. int[] heights = new int[row];
  10. // 以每一列的元素为基准,计算对应的高,(即1的连续个数),总共执行column次的面积计算
  11. for(int colIndex=0; colIndex<column;colIndex++){
  12. for(int rowIndex=0; rowIndex<row;rowIndex++){
  13. int tmp = colIndex;
  14. while(tmp<column&&matrix[rowIndex][tmp]=='1'){
  15. heights[rowIndex]++;
  16. tmp++;
  17. }
  18. }
  19. countArea(heights);
  20. // 计算一次面积后,heights中元素置0
  21. for(int i =0;i<row;i++){
  22. heights[i]=0;
  23. }
  24. }
  25. return area;
  26. }
  27. public void countArea(int[] heights){
  28. int len = heights.length;
  29. int[] copy = new int[len+2];
  30. System.arraycopy(heights,0,copy,1,len);
  31. Stack<Integer> stack = new Stack<Integer>();
  32. for(int i=0;i<copy.length;i++){
  33. while(!stack.empty()&&copy[stack.peek()]>copy[i]){
  34. int index = stack.pop();
  35. area = Math.max(area, (i-stack.peek()-1)*copy[index]);
  36. }
  37. stack.push(i);
  38. }
  39. }
  40. }