zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    nginx配置zabbix下setup.php(web页面)无法显示,浏览器无法打开
    CentOS release 6.5下jdk1.7升级到1.8
    tcp流量控制
    图像处理服务器
    muduo rpc protobuf 实现学习
    p2p nat 穿透原理
    博客-livevent-stl-cpp-nginx
    使用eventfd创建一个用于事件通知的文件描述符
    多线程设计的类的思考!
    ftp协议服务器与tinyhttp服务demo
  • 原文地址:https://www.cnblogs.com/qmillet/p/12035413.html
Copyright © 2011-2022 走看看