剑指offer题解16-20(Java)

16.合并两个排序的链表

输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。

代码

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode Merge(ListNode list1, ListNode list2) {
        ListNode newlist = new ListNode(0);
        ListNode cur = newlist;

        while (list1 != null && list2 != null) {
            if (list1.val <= list2.val) {
                cur.next = new ListNode(list1.val);
                list1 = list1.next;
            } else {
                cur.next = new ListNode(list2.val);
                list2 = list2.next;
            }
            cur = cur.next;
        }
        while (list1 != null) {
            cur.next = list1;
            list1 = list1.next;
            cur = cur.next;
        }
        while (list2 != null) {
            cur.next = list2;
            list2 = list2.next;
            cur = cur.next;
        }
        return newlist.next;

    }
}

17.输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)

代码

树的知识还是欠缺

public class Solution {
    //遍历大树
    public boolean HasSubtree(TreeNode root1,TreeNode root2) {
        if(root1 == null || root2 == null){
            return false;
        }
        //如果找到与子树相同根的值,走判断方法
        if(root1.val == root2.val){
            if(judge(root1,root2)){
                return true;
            }
        }
        //遍历左孩子,右孩子
        return HasSubtree(root1.left, root2) || HasSubtree(root1.right, root2);
    }
     
    //判断是否是子结构
    public boolean judge(TreeNode root, TreeNode subtree) {
        //子结构已经循环完毕,代表全部匹配
        if(subtree == null){
            return true;
        }
        //大树已经循环完毕,并未成功匹配
        if(root == null){
            return false;
        }
        //相等后判断左右孩子
        if(root.val == subtree.val){
            return judge(root.left, subtree.left) && judge(root.right, subtree.right);
        }
        return false;
    }
}

18.二叉树镜像

代码

/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
    public void Mirror(TreeNode root) {
        if(root!=null){
          //不为空左右子节点交换
            TreeNode temp=root.left;
            root.left=root.right;
            root.right=temp;
          //再对左右子树进行同样的操作
            Mirror(root.left);
            Mirror(root.right);
                
        }
      //为空,直接结束
        return;
        
    }
}

19.顺时针打印矩阵

代码

错误尝试

import java.util.ArrayList;

public class Solution {
    static ArrayList<Integer> ans;
    static int x;
    static int y;

    public ArrayList<Integer> printMatrix(int[][] matrix) {
        ans = new ArrayList<Integer>();
        x = matrix.length;
        y = matrix[0].length;
        int[][] vis = new int[x][y];
        dfs(matrix, vis, 0, 0);
        return ans;
    }
    public void dfs(int[][] matrix, int[][] vis, int i, int j,int flag) {
        if ((i >= 0 && i < x) && (j >= 0 && j < y)) {
            if (vis[i][j] == 0) {
                ans.add(matrix[i][j]);
                vis[i][j] = 1;
              flag switch{
                case 1:
                  
              }
                dfs(matrix, vis, i, j + 1);
                dfs(matrix, vis, i + 1, j);
                dfs(matrix, vis, i, j - 1);
                dfs(matrix, vis, i - 1, j);
            }
        }
        return;
    }
}

向上的时候出现了问题

换个思路

import java.util.ArrayList;
 
public class Solution {
 
    // 走的方向:向右、向下、向左、向上
    private final int[] dx = {0, 1, 0, -1};
    private final int[] dy = {1, 0, -1, 0};
 
    public ArrayList<Integer> printMatrix(int[][] matrix) {
        int n = matrix.length, m = matrix[0].length;
        boolean[][] vis = new boolean[n][m];
        ArrayList<Integer> list = new ArrayList<>();
 
        int x = 0, y = 0, dir = 0;
        while (x >= 0 && x < n && y >= 0 && y < m && !vis[x][y]) {
            list.add(matrix[x][y]);
            vis[x][y] = true;
 
            // 试着继续向dir的方向走
            while (x + dx[dir] >= 0 && x + dx[dir] < n && y + dy[dir] >= 0 && y + dy[dir] < m && !vis[x + dx[dir]][y + dy[dir]]) {
                x += dx[dir];
                y += dy[dir];
                list.add(matrix[x][y]);
                vis[x][y] = true;
            }
            // 走不动了换方向
            dir = (dir + 1) % 4;
            x += dx[dir];
            y += dy[dir];
        }
        return list;
    }
}

20.包含min函数的栈

定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。

代码

import java.util.Stack;

public class Solution {

    private static Stack<Integer> stack = new Stack<Integer>();
    //存储历史最小值
    private static Stack<Integer> minStack = new Stack<Integer>();
    private static Integer min = Integer.MAX_VALUE;

    public void push(int node) {
        //当前值小于min,则更新min
        if (node < min) {
            min = node;
            //在历史最小值的栈中加入
            minStack.push(node);
        }
        stack.push(node);

    }

    public void pop() {
        if (stack.peek().equals(min)) {
            //当前值为最小值,则需要更新一下当前的最小值
            minStack.pop();
            min = minStack.peek();
        }
        stack.pop();

    }

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

    }

    public int min() {
        return min;

    }
}
发布了36 篇原创文章 · 获赞 8 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_42297075/article/details/104360767