算法随笔 (五)

判断平衡二叉树
    def isBalanced(self, root: TreeNode) -> bool:
        def recur(root):
            if not root: return 0;
            # 左右子树不平衡直接返回 -1
            left = recur(root.left)
            if left == -1 :return -1
            right = recur(root.right)
            if right == -1 :return -1
            # 如果平衡则加一,如果不平衡返回-1
            return max(left, right) + 1 if abs(left - right) <= 1 else -1
        return recur(root) != -1
循环打印矩阵
 public static int[] spiralOrder(int[][] matrix) {
        if(matrix.length == 0 || matrix[0].length == 0){
            return new int[0];
        }
        int row = matrix.length;
        int col = matrix[0].length;
        int size = row*col;
        int[] res = new int[size];
        int index = 0;
        int left = 0, top = 0, right = col - 1, bottom = row - 1;
        while(index < size){
            //从左往右走
            for (int i = left; i <= right; i++) {
                res[index++] = matrix[top][i];
            }
            if (++top > bottom) {
                break;
            }
            //从上往下走
            for (int i = top; i <= bottom; i++) {
                res[index++] = matrix[i][right];
            }
            if (--right < left) {
                break;
            }
            //从右往左走
            for (int i = right; i >= left; i--) {
                res[index++] = matrix[bottom][i];
            }
            if (--bottom < top) {
                break;
            }
            //从下往上走
            for (int i = bottom; i >= top; i--) {
                res[index++] = matrix[i][left];
            }
            if (++left > right) {
                break;
            }
        }
        return res;
    }
合并有序链表
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode head = new ListNode(-1);
        ListNode index = head;
        while(l1 != null && l2 != null){
            if(l1.val < l2.val){
                index.next = l1;
                index = l1;
                l1 = l1.next;
            }else{
                index.next = l2;
                index = l2;
                l2 = l2.next;
            }
        }
        if(l1 != null){
            index.next = l1;
        }
        if(l2 != null){
            index.next = l2;
        }
        return head.next;
    }
矩阵路径
 public boolean exist(char[][] board, String word) {
        if (board == null || board[0] == null || board.length == 0 || board[0].length == 0 || word == null || word.equals("")) {
            return false;
        }
        boolean[][] isVisited = new boolean[board.length][board[0].length];
        char[] chs = word.toCharArray();
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                if (board[i][j] == chs[0]) {
                    if (bfs(board, i, j, isVisited, chs, 0)) return true;
                }
            }
        }
        return false;
    }

     private boolean bfs(char[][] board, int i, int j, boolean[][] isVisited, char[] chs, int index) {
        if (index == chs.length) {
            return true;
        }
        if (i < 0 || j < 0 || i == board.length || j == board[0].length || isVisited[i][j] || board[i][j] != chs[index]) {
            return false;
        }
        isVisited[i][j] = true;
        boolean res = bfs(board, i + 1, j, isVisited, chs, index + 1)
                || bfs(board, i - 1, j, isVisited, chs, index + 1)
                || bfs(board, i, j + 1, isVisited, chs, index + 1)
                || bfs(board, i, j - 1, isVisited, chs, index + 1);
        isVisited[i][j] = false;
        return res;
    }

判断入栈出栈
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        if(pushed.length != popped.length){
            return false;
        }
        Map<Integer,Integer> dicMap = new HashMap(pushed.length);
        for(int i = 0;i<pushed.length;i++){
            dicMap.put(pushed[i],i);
        }
        int res = 0;
        for(int i = 0;i<popped.length-1;i++){
            int one = dicMap.get(popped[i]);
            int two = dicMap.get(popped[i+1]);
            if (one < two && res > two){
                return false;
            }
            res = Math.max(dicMap.get(popped[i]),res);
        }
        return true;
    }
构建二叉树

Arrays.copyOfRange(T[ ] original,int from,int to)
[from,to)

 public TreeNode buildTree(int[] preorder, int[] inorder) {
        if(preorder.length == 0){
            return null;
        }
        //找到中序遍历的头结点
        int root = 0;
        for(int i = 0;i < inorder.length;i++){
            if(preorder[0] == inorder[i]){
                root = i;
            }
        }
        TreeNode rootNode = new TreeNode(preorder[0]);
        rootNode.left = buildTree(Arrays.copyOfRange(preorder,1,root+1),
        Arrays.copyOfRange(inorder,0,root));
        rootNode.right = buildTree(Arrays.copyOfRange(preorder,root+1,preorder.length),
        Arrays.copyOfRange(inorder,root+1,inorder.length));
        return rootNode;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值