• 11.面试思路&画图让抽象具体化(2)


    面试思路

    题一:【二叉树的镜像】

    操作给定的二叉树,将其变换为源二叉树的镜像。
    分析:使用递归=》边界条件:节点为空,交换当前节点的左右节点。
     1 /**
     2 public class TreeNode {
     3     int val = 0;
     4     TreeNode left = null;
     5     TreeNode right = null;
     6 
     7     public TreeNode(int val) {
     8         this.val = val;
     9 
    10     }
    11 
    12 }
    13 */
    14 public class Solution {
    15     public void Mirror(TreeNode root) {
    16         if(root==null) return;
    17         TreeNode temp = root.left;
    18         root.left = root.right;
    19         root.right = temp;
    20         Mirror(root.left);
    21         Mirror(root.right);
    22     }
    23 }

     画图让抽象形象化

    题一:【顺时针打印矩阵】

    输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.

    分析:画图总结:定义四个变量

     1 import java.util.ArrayList;
     2 public class Solution {
     3     public ArrayList<Integer> printMatrix(int [][] matrix) {
     4         int a=0,b=matrix[0].length-1,c=matrix.length-1,d=0;
     5         ArrayList<Integer> list = new ArrayList<Integer>();
     6         while(a<=c&&d<=d){
     7             for(int i=d;i<=b&&a<=c;i++){
     8                 list.add(matrix[a][i]);
     9             }
    10             a++;
    11             for(int i=a;i<=c&&d<=b;i++){
    12                 list.add(matrix[i][b]);
    13             }
    14             b--;
    15             for(int i=b;i>=d&&a<=c;i--){
    16                 list.add(matrix[c][i]);
    17             }
    18             c--;
    19             for(int i=c;i>=a&&d<=b;i--){
    20                 list.add(matrix[i][d]);
    21             }
    22             d++;
    23         }
    24         return list;
    25     }
    26 }
  • 相关阅读:
    Tomcat安装和配置过程
    Java集合框架概述
    Hash表的原理
    Java 浅拷贝和深拷贝的理解和实现方式
    Nginx 配置上传文件大小
    将博客搬至CSDN
    vscode中设置vue代码片段
    底部标签栏获取token失败
    Eacharts K线报错问题
    阿里字体图标库在项目中引用
  • 原文地址:https://www.cnblogs.com/qmillet/p/12035413.html
Copyright © 2020-2023  润新知