zoukankan      html  css  js  c++  java
  • 顺时针打印矩阵

    question:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下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.

    思路:根据最后一圈打印的情况分情况讨论

     resolution:
     

    import com.sun.org.apache.xalan.internal.xsltc.dom.MatchingIterator;
    
    import javax.xml.ws.soap.MTOM;
    import java.util.ArrayList;
    import java.util.jar.JarEntry;
    
    public class Test3 {
    
        public ArrayList<Integer> printMatrix(int [][] matrix) {
            ArrayList<Integer> list = new ArrayList<>();
    
            //求出矩阵的行和列数
            int row = matrix.length;
            int col = matrix[0].length;
            //定义4个变量分别表示行和列的起始和终止位置
            //第一圈的值
            int startRow = 0;
            int endRow = row - 1;
            int startCol = 0;
            int endCol = col - 1;
            //开始循环打印,通过while来控制循环的终止条件
            while (startRow <= endRow && startCol <= endCol){
    
                //判断是否为特殊情况,注意这里需要先处理特殊情况
                //只剩下一行
                if(startRow == endRow){
                    for(int j = startCol; j <= endCol; j++){
                        list.add(matrix[startRow][j]);
                    }
                    //如果为特殊情况处理之后就要返回回去了,不然还会继续跳入其他循环体
                    return list;
                }
                //只剩下一列
                if(startCol == endCol){
                    for(int i = startRow; i <= endRow; i++){
                        list.add(matrix[i][startCol]);
    
                    }
                    return list;
                }
                //只剩下一个数
                if(startRow == endRow && startCol == endCol){
                    list.add(matrix[startRow][startCol]);
                    return list;
                }
    
    
                //开始打印第一行 使用for循环 可以定义起始和终止位置
                for(int j = startCol;j <= endCol;j++){
                    list.add(matrix[startRow][j]);
                }
                //打印最后一列 注意起始位置不要重复打印
                for(int i = startRow + 1; i <= endRow; i++){
                    list.add(matrix[i][endCol]);
                }
                //打印最后一行
                for(int j = endCol - 1; j >= startCol; j--){
                    list.add(matrix[endRow][j]);
                }
                //打印第一列
                for(int i = endRow - 1; i > startRow; i--){
                    list.add(matrix[i][startCol]);
                }
    
    
                //修改变量
                startRow += 1;
                endRow -= 1;
                startCol += 1;
                endCol -= 1;
            }
    
            return list;
    
        }
    
        public static void main(String[] args){
            int[][] matrix = {{1,2,3},{5,6,7},{9,10,11}};
            int rows = matrix.length;
            int cols = matrix[0].length;
    
            for(int i = 0; i < rows; i++){
                for(int j = 0; j < cols; j++){
                    System.out.print(matrix[i][j] + " ");
                }
                System.out.println("
    ");
    
            }
            ArrayList<Integer> list = new Test3().printMatrix(matrix);
            for(int i = 0; i < list.size(); i++){
    
                System.out.println(list.get(i));
            }
    
        }
    /**运行结果测试示例1
     * 1 2 3 4
    
     5 6 7 8
    
     9 10 11 12
    
     1
     2
     3
     4
     8
     12
     11
     10
     9
     5
     6
     7
     运行结果测试示例2
     1 2 3 
    
     5 6 7 
    
     9 10 11 
    
     1
     2
     3
     7
     11
     10
     9
     5
     6
     */
    
    
    
    }
    
  • 相关阅读:
    定时器应用(函数封装)
    js中的作用域
    js函数传参
    js数据类型转换
    jQuery总结
    少些招数,多些内力
    浏览器中的标签切换事件
    正则表达式之小有名气
    正则表达式之初入江湖
    详解apply
  • 原文地址:https://www.cnblogs.com/flyingcr/p/10326829.html
Copyright © 2011-2022 走看看