leetcode (栈->中等) 71,94,150,173,227,331

71

  思路是先用split方法按"/"分割,这样多个/连一起的字符串就会被分割为空 就可以直接和"."一样跳过处理

class Solution {
    public String simplifyPath(String path) {
  LinkedList<String> stack = new LinkedList<>();
        String[] split = path.split("/");
        for (String s : split) {
            if (s.equals("") || s.equals(".")){
                continue;
            }

            if (!stack.isEmpty()){
                if (s.equals("..")){
                    stack.pop();
                }else {
                    stack.push(s);
                }
            }else {
                if (!s.equals("..")){
                    stack.push(s);
                }
            }
        }
        StringBuilder sb = new StringBuilder();
        if (stack.isEmpty()){
            return "/";
        }
        while (!stack.isEmpty()){
            sb.append("/").append(stack.removeLast());

        }


        return sb.toString();
    }
}

94

        LinkedList<TreeNode> stack = new LinkedList<>();
        List<Integer> result = new ArrayList<>();
        if (root == null){
            return result;
        }
        stack.push(root);
        while (!stack.isEmpty()){
            TreeNode pop = stack.pop();
            TreeNode left =  pop.left;
            if (left != null){
                pop.left = null;
                stack.push(pop);
                stack.push(left);
                continue;
            }
            result.add(pop.val);
            System.out.println(pop.val);
            TreeNode right =  pop.right;
            if (right != null){
                stack.push(right);
            }
        }

        return result;

103  

 public static List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        Map<Integer,List<Integer>> data = new HashMap<>();
        List<List<Integer>> result = new ArrayList<>();
        if (root == null){
            return result;
        }
        LinkedList<TreeNode> stack = new LinkedList<>();
        stack.push(root);
        int k = 0;
        while (!stack.isEmpty()){
            TreeNode pop = stack.pop();
            TreeNode left = pop.left;
            if (left != null){
                pop.left = null;
                stack.push(pop);
                stack.push(left);
                k++;
                continue;
            }
            TreeNode right = pop.right;
            if (right != null){
                pop.right = null;
                stack.push(pop);
                stack.push(right);
                k++;
                continue;
            }

            List<Integer> integers = data.get(k);
            if (integers == null){
                integers = new ArrayList<>();
                data.put(k,integers);
            }
            integers.add(pop.val);
            k--;
        }
        for (int i = 0; i < data.size(); i++) {
            List<Integer> integers = data.get(i);
            if (i % 2 == 0){
                result.add(integers);
            }else {
                Collections.reverse(integers);
                result.add(integers);
            }

        }
        return result;
    }

150

    public static int evalRPN(String[] tokens) {

        LinkedList<Integer> stack = new LinkedList<>();
        for (String token : tokens) {
            if (token.equals("+")){
                stack.push(stack.pop()+stack.pop());
            }else  if (token.equals("-")){
                stack.push(-(stack.pop()-stack.pop()));
            }else if (token.equals("*")){
                stack.push(stack.pop()*stack.pop());
            }else if (token.equals("/")){
                Integer pop = stack.pop();
                Integer pop1 = stack.pop();
                stack.push(pop1/pop);
            }else {
                stack.push(Integer.valueOf(token));
            }
        }

        return stack.pop();
    }

173

  其实就是中序遍历二叉树

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class BSTIterator {

    LinkedList<TreeNode> stack;

    public BSTIterator(TreeNode root) {
        stack = new LinkedList<>();
        if (root != null)
        stack.push(root);
    }
    
    public int next() {
 while (!stack.isEmpty()){
            TreeNode pop = stack.pop();
            TreeNode left = pop.left;
            if (left != null){
                stack.push(pop);
                stack.push(left);
                pop.left = null;
                continue;
            }

            TreeNode right = pop.right;
            if (right != null){
                stack.push(right);
            }
            return pop.val;

        }
        return 0;
    }
    
    public boolean hasNext() {
 return !stack.isEmpty();
    }
}

227

 public static int calculate(String s) {
        s = s.replace(" ", "");
        LinkedList<Integer> stack = new LinkedList<>();
        int length = s.length();
        int sigh = 0;//0数字, 1 + ,2 -,3 *,4 /
        for (int i = 0; i < length; i++) {
            char c = s.charAt(i);
            if (c >= 48 && c <= 57){
                int k = c-48;
                if (i == 0 ||sigh == 1){
                    stack.push(k);
                }else if (sigh == 2){
                    stack.push(-k);
                }else if (sigh == 0){
                    Integer pop = stack.pop();
                    pop = pop<=0 ? pop*10-k:pop*10+k;
                    stack.push(pop);
                }else {
                    while (i < length-1 && s.charAt(i +1) >= 48 && s.charAt(i +1) <= 57){
                        k = k*10+(s.charAt(i +1)-48);
                        i++;
                    }
                    Integer pop = stack.pop();
                    stack.push(sigh == 3 ?pop*k:pop/k);

                }
                sigh = 0;
            }else {
                if (c == '+'){
                    sigh = 1;
                }else if (c == '-'){
                    sigh = 2;
                }else if (c == '*'){
                    sigh = 3;
                }else if (c == '/'){
                    sigh = 4;
                }
            }
        }
        int total = 0;
        while (!stack.isEmpty()){
            total += stack.pop();
        }

        return total;
    }

331  这儿借鉴了大佬的思路就是将#理解为nil节点,那么nil节点始终应该是非nil节点多1个的

class Solution {
    public boolean isValidSerialization(String preorder) {
        String[] split = preorder.split(",");
        int k = 0;
        for (int i = 0; i < split.length; i++) {
            if (k <0){
                return false;
            }
            if (!split[i].equals("#")){
                k++;
            }else {
                k--;
            }
        }
        return k == -1;
    }
}
原文地址:https://www.cnblogs.com/hetutu-5238/p/14317982.html