zoukankan      html  css  js  c++  java
  • LeetCode---Stack && Heap

    402. Remove K Digits
    思路:一次判断字符若比栈顶字符大则入栈,若小则pop,同时k--,直到k为0,注意最终k没有减为0或者中途栈为空或者最终结果前面带0的情况
    
    public String removeKdigits(String num, int k) {
        if(k == num.length()) return "0";
        Stack<Character> s = new Stack<Character>();
        int i = 0;
        while(i < num.length()){
            while(!s.isEmpty() && k > 0 && num.charAt(i) < s.peek()){
                s.pop();
                k--;
            }
            s.push(num.charAt(i));
            i++;
        }
        
        StringBuffer str = new StringBuffer();
        while(!s.isEmpty()){
            str = str.append(s.pop());
        }
        str = str.reverse();
        int j = 0;
        while(j < str.length() && str.charAt(j) == '0'){
            j++;
        }
        //有可能因为后面有递增序列k没有减为0
        if(k > 0) return str.substring(0,str.length() - k);
        //将前面的0去掉后有可能为""
        return str.substring(j).equals("") ? "0" : str.substring(j);
    }
    
    224. Basic Calculator
    思路:用sign标记前面的运算符,用result标记括号中的结果,每次遇到'('则将sign和result入栈并reset,遇到')'则计算结果
    
    public int calculate(String s) {
        int result = 0;
        int sign = 1;
        Stack<Integer> stack = new Stack<Integer>();
        for(int i = 0; i < s.length(); i++){
            if(Character.isDigit(s.charAt(i))){
                //直接计算括号中的运算
                int num = s.charAt(i) - '0';
                while(i + 1 < s.length() && Character.isDigit(s.charAt(i + 1))){
                    num = num * 10 + (s.charAt(i + 1) - '0');
                    i++;
                }
                result += num * sign;
            }
            else if(s.charAt(i) == '+') sign = 1;
            else if(s.charAt(i) == '-') sign = -1;
            else if(s.charAt(i) == '('){
                stack.push(result);
                stack.push(sign);
                result = 0;
                sign = 1;
            }
            else if(s.charAt(i) == ')'){
                result = result * stack.pop() + stack.pop();
            }
        }
        return result;
    }
    
    总结
    232. Implement Queue using Stacks:用两个栈实现队列
    225. Implement Stack using Queues:用两个队列实现栈
    150. Evaluate Reverse Polish Notation:遇到运算符则获取两个数进行运算以后入栈
    341. Flatten Nested List Iterator:从后往前入栈,然后每次讲顶上的list转化为Integer入栈
    239. Sliding Window Maximum:用优先队列将个窗口放入然后取出最大值
    373. Find K Pairs with Smallest Sums:用优先队列将所有组合放入,然后取出前k个最小的
    215. Kth Largest Element in an Array:用优先队列放入所有元素,取出第K大的,用优先队列比用快速排序快很多
    
    训练
    155. Min Stack:使用栈每次存当前最小值和入栈的元素,更新最小值
    331. Verify Preorder Serialization of a Binary Tree:每次入栈同时出现两个'#'则出栈上一个元素并入一个'#',最后栈中只有一个'#'则返回true,注意中途判断栈空
    394. Decode String:遇到']'才开始一次出栈,先获取需要被重复的字符串,然后获取次数,再入栈
    295. Find Median from Data Stream:用两个优先队列min和max,保证每个队列都有一半元素且min中的元素比max中的小
    
    提示
    使用栈注意在中途出栈时判断栈空
  • 相关阅读:
    CodeForces-455A Boredom
    UVA-12627 Erratic Expansion
    汉诺塔系列问题
    CodeForces-999D Equalize the Remainders
    CodeForces-1061D TV Shows
    CodeForces-1061B Views Matter
    UVALive-7261 Xiongnu's Land
    HDU-4990 Reading comprehension
    -------------------------------用MyBatis处理表与表之间的关联关系----------------------------------
    -------------计算机里面算法-----------
  • 原文地址:https://www.cnblogs.com/LeonNew/p/6248889.html
Copyright © 2011-2022 走看看