leetcode 堆栈

来源:互联网 发布:蕨类植物 分形 算法 编辑:程序博客网 时间:2024/06/08 00:12

1、Evaluate Reverse Polish Notation
链接:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/

思路:使用栈,数字进栈,符号出栈,注意数字可能有负数,注意判断条件

public int evalRPN(String[] tokens) {        Stack stack = new Stack();        int len = tokens.length;        int a, b, s;        for(int i = 0; i < len; i++){            switch(tokens[i]){                case "+":                    b = (int)stack.pop();                    a = (int)stack.pop();                    s = a + b;                    stack.push(s);                    break;                case "-":                    b = (int)stack.pop();                    a = (int)stack.pop();                    s = a - b;                    stack.push(s);                    break;                case "*":                    b = (int)stack.pop();                    a = (int)stack.pop();                    s = a * b;                    stack.push(s);                    break;                case "/":                    b = (int)stack.pop();                    a = (int)stack.pop();                    s = a / b;                    stack.push(s);                    break;                default:stack.push(Integer.parseInt(tokens[i]));break;            }        }        return (int)stack.pop();    }

2、Longest Valid Parentheses
链接:http://oj.leetcode.com/problems/longest-valid-parentheses/

思路:利用栈,为了便于求最长串,需要继续字符的下标。‘(’入栈,’)’如果栈顶是‘(’,出栈,否则,入栈。

public class Kuohao{        int i;        char ch;        public Kuohao(int i,char ch){            this.i = i;            this.ch = ch;        }    }    public int longestValidParentheses(String s) {        if(s.length()<=1)            return 0;        Stack<Kuohao> stack = new Stack();        int len = s.length();        int temp, result = 0;        for(int i = 0; i < len; i++) {            if (s.charAt(i) == '(') {                stack.push(new Kuohao(i, '('));            } else {                if (!stack.isEmpty() && stack.peek().ch == '(') {                    stack.pop();                    if (stack.isEmpty()) {                        temp = i + 1;                    } else {                        temp = i - stack.peek().i;                    }                    result = result > temp ? result : temp;                }else {                    stack.push(new Kuohao(i,')'));                }            }        }        return result;    }

3、Valid Parentheses
链接:http://oj.leetcode.com/problems/valid-parentheses/
思路:注意添加栈不为空的条件

public boolean isValid(String s) {        if (s.length() == 0)            return true;        if(s.length() <= 1)            return false;        Stack stack = new Stack();        for(int i = 0; i < s.length(); i++){            switch (s.charAt(i)){                case '(':                case '[':                case '{':                    stack.push(s.charAt(i));                    break;                case ')':                    if(!stack.isEmpty() && stack.peek().equals('(')){                        stack.pop();                        break;                    }else return false;                case ']':                    if(!stack.isEmpty() && stack.peek().equals('[')){                        stack.pop();                        break;                    }else return false;                case '}':                    if(!stack.isEmpty() && stack.peek().equals('{')){                        stack.pop();                        break;                    }else return false;            }        }        if(stack.isEmpty())            return true;        return false;    }

4、Largest Rectangle in Histogram
链接:http://oj.leetcode.com/problems/largest-rectangle-in-histogram/

思路:递归思想,将局部最大值保存下来;也可以利用栈来求解

    public int largestRectangleArea(int[] heights) {        int len = heights.length;        int result = 0;        for(int i = 0; i < len; i++){            if(i + 1 < len  && heights[i] <= heights[i + 1]){                continue;            }            int area, min = heights[i];            for(int j = i; j >= 0; j--){                min = heights[j] < min ? heights[j] : min;                area = min * (i - j + 1);                result = area > result ? area : result;            }        }        return result;    }

5、Trapping Rain Water
链接:http://oj.leetcode.com/problems/trapping-rain-water/
思路:http://www.cnblogs.com/grandyang/p/4322653.html
需要left和right两个指针分别指向数组的首尾位置,从两边向中间扫描,在当前两指针确定的范围内,先比较两头找出较小值,如果较小值是left指向的值,则从左向右扫描,如果较小值是right指向的值,则从右向左扫描,若遇到的值比当较小值小,则将差值存入结果,如遇到的值大,则重新确定新的窗口范围,以此类推直至left和right指针重合。

public int trap(int[] height) {        int result = 0;        int left = 0, right = height.length - 1;        int min = 0;        while(left < right){            min = height[left] < height[right] ? height[left] : height[right];            if(height[left] == min){                left++;                while(left < right && height[left] < min){                    result += (min - height[left]);                    left++;                }            }else {                right--;                while(left < right && height[right] < min){                    result += (min - height[right]);                    right--;                }            }        }        return result;    }
1 0
原创粉丝点击