27-31

剑指Offer_27_二叉树的镜像

/**
 * https://leetcode-cn.com/problems/er-cha-shu-de-jing-xiang-lcof/
 *
 * @author Qitong!!
 * @Date 2020/7/3
 */
public class 剑指Offer_27_二叉树的镜像 {
    public TreeNode mirrorTree(TreeNode root) {
        if (root == null) return null;
        //先序遍历  处理逻辑!!
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;

        mirrorTree(root.left);
        mirrorTree(root.right);
        return root;
    }
}

剑指Offer_28_对称的二叉树


/**
 * https://leetcode-cn.com/problems/dui-cheng-de-er-cha-shu-lcof/
 *
 * @author Qitong!!
 * @Date 2020/7/3
 */
public class 剑指Offer_28_对称的二叉树 {
    public boolean isSymmetric(TreeNode root) {
        //分开 进行!
        return isMirror(root, root);
    }

    private boolean isMirror(TreeNode lt, TreeNode rt) {
        if (lt == null && rt == null) return true;
        if (lt == null || rt == null) return false;
        //判断 这两个的值 是否相等   ,并且判断 左树的左子树和右树的右子树 ,左树的右子树和右树的左子树是否相等
        return lt.val == rt.val && isMirror(lt.left, rt.right) && isMirror(rt.left, lt.right);
    }
}

剑指Offer_29_顺时针打印矩阵

/**
 * https://leetcode-cn.com/problems/shun-shi-zhen-da-yin-ju-zhen-lcof/
 *
 * @author Qitong!!
 * @Date 2020/7/3
 */
public class 剑指Offer_29_顺时针打印矩阵 {
    public static int[] spiralOrder(int[][] matrix) {
        if (matrix == null) return null;
        if (matrix.length == 0) return new int[]{};
        int[] res = new int[matrix.length * matrix[0].length];

        int top = 0;
        int bottom = matrix.length - 1;
        int left = 0;
        int right = matrix[0].length - 1;
        //res 的索引位置!!
        int cur = 0;
        while (top <= bottom && left <= right) {
            //从 左上 到 右上
            for (int i = left; i <= right; i++) {
                res[cur++] = matrix[top][i];
            }
            top++;
            //从 右上 到 右下
            for (int i = top; i <= bottom; i++) {
                res[cur++] = matrix[i][right];
            }
            right--;

            if (top > bottom || left > right) break;

            //从 右下 到 左下
            for (int i = right; i >= left; i--) {
                res[cur++] = matrix[bottom][i];
            }
            bottom--;
            //从 左下 到 左上
            for (int i = bottom; i >= top; i--) {
                res[cur++] = matrix[i][left];
            }
            left++;
        }

        return res;
    }

    public static void main(String[] args) {
        int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        System.out.println(Arrays.toString(spiralOrder(matrix)));
    }
}

剑指Offer_30_包含min函数的栈

/**
 * https://leetcode-cn.com/problems/bao-han-minhan-shu-de-zhan-lcof/
 *
 * @author Qitong!!
 * @Date 2020/7/3
 */
public class 剑指Offer_30_包含min函数的栈 {

}

class MinStack {
    private Stack<Integer> stack;
    private Stack<Integer> minStack;

    /**
     * initialize your data structure here.
     */
    public MinStack() {
        stack = new Stack<>();
        minStack = new Stack<>();
    }

    public void push(int x) {
        stack.push(x);
        if (minStack.isEmpty()) minStack.push(x);
        else {
            minStack.push(x > minStack.peek() ? minStack.peek() : x);
        }
    }

    public void pop() {
        stack.pop();
        minStack.pop();
    }

    public int top() {
        return stack.peek();
    }

    public int min() {
        return minStack.peek();
    }
}

剑指Offer_31_栈的压入_弹出序列

/**
 * https://leetcode-cn.com/problems/zhan-de-ya-ru-dan-chu-xu-lie-lcof/
 *
 * @author Qitong!!
 * @Date 2020/7/3
 */
public class 剑指Offer_31_栈的压入_弹出序列 {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        Stack<Integer> stack = new Stack<>();
        //为 匹配到popped的 位置
        int i = 0;
        for (int num : pushed) {
            stack.push(num);
            while (!stack.isEmpty() && stack.peek() == popped[i]) {
                stack.pop();
                i++;
            }
        }
        return stack.isEmpty();
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_45399846/article/details/107558676