Min Stack


class MinStack {
    // stack: store the stack numbers
    private Stack<Integer> stack = new Stack<Integer>();
    // minStack: store the current min values
    private Stack<Integer> minStack = new Stack<Integer>();
    public void push(int x) {
        // store current min value into minStack
        if (minStack.isEmpty() || x <= minStack.peek())
            minStack.push(x);
        stack.push(x);
    }
    public void pop() {
        // use equals to compare the value of two object, if equal, pop both of them
        if (stack.peek().equals(minStack.peek()))
            minStack.pop();
        stack.pop();
    }
    public int top() {
        return stack.peek();
    }
    public int getMin() {
        return minStack.peek();
    }
}

class MinStack {
    class Node{
        int value;
        int min;
        Node next;
        Node(int x, int min){
            this.value=x;
            this.min=min;
            next = null;
        }
    }
    Node head;
    public void push(int x) {
        if(null==head){
            head = new Node(x,x);
        }else{
            Node n = new Node(x, Math.min(x,head.min));
            n.next=head;
            head=n;
        }
    }
    public void pop() {
        if(head!=null)
            head =head.next;
    }
    public int top() {
        if(head!=null)
            return head.value;
        return -1;
    }
    public int getMin() {
        if(null!=head)
            return head.min;
        return -1;
    }
}

Implement Queue using Queues


class MyStack {
    Queue<Integer> q = new LinkedList<Integer>();
    // Push element x onto stack.
    public void push(int x) {
        q.add(x);
    }
    // Removes the element on top of the stack.
    public void pop() {
        int size = q.size();
        for(int i = 1; i < size; i++){
            q.add(q.remove());
        }
        q.remove();
    }
    // Get the top element.
    public int top() {
        int size = q.size();
        for(int i = 1; i < size; i++){
            q.add(q.remove());
        }
        int ret = q.remove();
        q.add(ret);
        return ret;
    }
    // Return whether the stack is empty.
    public boolean empty() {
        return q.isEmpty();        
    }
}

Implement Queue Using Stacks


class MyQueue {
    Stack<Integer> s1 = new Stack();
    Stack<Integer> s2 = new Stack();
    // Push element x to the back of queue.
    public void push(int x) {
        while (!s2.isEmpty()){
            s1.push(s2.pop());
        }
        s1.push(x);
    }
    // Removes the element from in front of queue.
    public void pop() {
        while (!s1.isEmpty()){
            s2.push(s1.pop());
        }
        s2.pop();
    }
    // Get the front element.
    public int peek() {
        while (!s1.isEmpty()){
            s2.push(s1.pop());
        }
        return s2.peek();
    }
    // Return whether the queue is empty.
    public boolean empty() {
        return s1.isEmpty() && s2.isEmpty();
    }
}

Basic Calculator


public class Solution {
    public int calculate(String s) {
        Stack<Integer> stack = new Stack<Integer>();
        int result = 0;
        int number = 0;
        int sign = 1;
        for(int i = 0; i < s.length(); i++){
            char c = s.charAt(i);
            if(Character.isDigit(c)){
                number = 10 * number + (int)(c - '0');
            }else if(c == '+'){
                result += sign * number;
                number = 0;
                sign = 1;
            }else if(c == '-'){
                result += sign * number;
                number = 0;
                sign = -1;
            }else if(c == '('){
                //we push the result first, then sign;
                stack.push(result);
                stack.push(sign);
                //reset the sign and result for the value in the parenthesis
                sign = 1;   
                result = 0;
            }else if(c == ')'){
                result += sign * number;  
                number = 0;
                result *= stack.pop();    //stack.pop() is the sign before the parenthesis
                result += stack.pop();   //stack.pop() now is the result calculated before the parenthesis

            }
        }
        if(number != 0) result += sign * number;
        return result;
    }
}

Largest Rectangle in Histogram


public class Solution {
    public int largestRectangleArea(int[] heights) {
        if(heights == null) return 0;
        if(heights.length == 0) return 0;
        int res = 0;
        //递增的单调栈
        LinkedList<Integer> s = new LinkedList<Integer>();
        s.push(-1);
        int len = heights.length;
        for(int i=0;i<len;i++){
            while(s.peek()>-1){
                if(heights[s.peek()]>heights[i]){
                    int top = s.pop();
                    //heights[top]和heights[s.peek()]中间的元素一定比heights[top]大
                    res = Math.max(res,heights[top]*(i-1-s.peek()));
                }else{
                    break;
                }
            }
            s.push(i);
        }
        while(s.peek()!=-1){
            int top = s.pop();
            res = Math.max(res,heights[top]*(len-1-s.peek()));
        }
        return res;
    }
}

Maximal Rectangle


public class Solution {
    public int maximalRectangle(char[][] matrix) {
        int area = 0, new_area, r, l;
        if(matrix.length > 0){
            int[] line = new int[matrix[0].length];
            boolean[] is_processed = new boolean[matrix[0].length];
            for(int i = 0; i < matrix.length; i++){
                for(int j = 0; j < matrix[i].length; j++){
                    if (matrix[i][j] == '1') {
                        line[j]++;
                        is_processed[j] = false;
                    } else {
                        line[j] = 0;
                        is_processed[j] = true;
                    }
                }
                for(int j = 0; j < matrix[i].length; j++){
                    if(is_processed[j]) continue;
                    r = l = 1;
                    while((j + r < line.length)&&(line[j + r] >= line[j])){
                        if(line[j + r] == line[j]) is_processed[j + r] = true;
                        r++;
                    }
                    while((j - l >= 0)&&(line[j - l] >= line[j])) l++;
                    new_area = (r + l - 1)*line[j];
                    if (new_area > area) area = new_area;
                }
            }
        } return area;
    }
}
posted on 2016-06-29 08:52  岳阳楼  阅读(156)  评论(0)    收藏  举报