zoukankan      html  css  js  c++  java
  • (剑指offer)顺时针打印矩阵

    题目描述

    输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下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 2 3
        4 5 6
        7 8 9
    输出并删除第一行后,变为
     
        4 5 6
        7 8 9
    再进行一次逆时针旋转,就变成:
     
        6 9
        5 8
        4 7
    继续重复上述操作即可。
    public class Solution {
        public ArrayList<Integer> printMatrix(int [][] matrix) {
            //作为存放结果的容器
            ArrayList<Integer> list = new ArrayList<>();
            //拿到出事数组的行数
            int row = matrix.length;
     
            while(row != 0){
                //将数组的第一行先添加进容器中
                for(int i=0;i<matrix[0].length;i++)
                    list.add(matrix[0][i]);
                //当行数等于1时就没有必要再继续执行了,在上面打印完之后就可以停止了
                if(row == 1)
                    break;
                //删除上面遍历的数组的第一行,然后旋转这个数组并返回
                matrix = revert(matrix);
                //更新行数
                row = matrix.length;
            }
     
            //返回
            return list;
        }
     
        private int[][] revert(int[][] matrix){
            //拿到matrix的行数和列数
            int rows = matrix.length;
            int cols = matrix[0].length;
     
            //因为我们要将原数组遍历过的第一行删除,然后旋转变成一个新的数组,所以先初始化一下这个新数组
            int[][] newMatrix = new int[cols][rows-1];
     
            //对这个新数组进行赋值
            for(int j=cols-1;j>=0;j--){
                for(int i=1;i<rows;i++){
                    newMatrix[cols-j-1][i-1] = matrix[i][j];
                }
            }
     
            //返回新数组
            return newMatrix;
        }
    }

    解法二:

    function printMatrix(matrix)
    {
        // write code here
        if(matrix.length ==0){
            return []
        }
        let start = 0
        let result = []
        let rows = matrix.length
        let colum = matrix[0].length
        while(colum > start*2 && rows > start*2){
            printMatrixCircle(matrix, colum, rows,start)
            start++
        }
        function printMatrixCircle(){
            let endX = colum-1-start //列-1
            let endY = rows-1-start
            //从左到右打印第一行
            for(let i = start; i <= endX; i++){
                result.push(matrix[start][i])
            }
            //从上到下打印一列
            if(start < endY){
                for(let i = start+1; i <= endY; i++){
                    result.push(matrix[i][endX])
                }
            }
            //从右到左打印
            if(start < endX && start < endY){
                for(let i = endX-1; i>=start; i--){
                    result.push(matrix[endY][i])
                }
            }
            //从下到上打印
            if(start < endX && start < endY -1){
                for(let i = endY-1; i>=start+1; i--){
                    result.push(matrix[i][start])
                }
            }
            return result
        }
        
    }

    解法三:

    function printMatrix(matrix) {
        if(matrix.length ==0){
            return []
        }
        let start = 0
        let result = []
        let rows = matrix.length
        let colum = matrix[0].length
        let top = 0, left = 0, right = colum-1, bottom = row-1;
            while(top <= bottom && left<= right){
                //从左到右
                for(let i = left; i <= right; ++i) result.push(matrix[top][i]);
                //从上到下
                for(let i = top+1; i <= bottom; ++i) result.push(matrix[i][right]);
                //从右到左
                for(let i = right-1; i >= left && top < bottom; --i) result.push(matrix[bottom][i]);
                //从下到上
                for(let i = bottom-1; i > top && right > left; --i) result.push(matrix[i][left]);
                ++top; ++left; --right; --bottom;
        }
        return result
    }

     

    不积跬步无以至千里
  • 相关阅读:
    Python3---内建函数---all()
    (dp)Codeforces Round #418 (Div. 2) C. An impassioned circulation of affection
    (状压dp)codevs2800 送外卖
    (dp)CF 813 Educational Codeforces Round 22 D. Two Melodies
    (线段树)CF813 Educational Codeforces Round 22 E
    (trie)HDU1251 统计难题
    (最大流)CodeForces
    (高斯消元)HDU2827 The Evaluation of Determinant
    (三分)HDU5531 Rebuild
    (并查集)Codeforces 325 D-Reclamation
  • 原文地址:https://www.cnblogs.com/lyt0207/p/12537225.html
Copyright © 2011-2022 走看看