顺时针打印矩阵

来源:互联网 发布:奇商网络 编辑:程序博客网 时间:2024/05/18 16:13

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

这里写图片描述

这个题感觉更像是找规律,我的思想是先求出从外到内一共有多少圈,然后去输入每一圈的四条边。
求圈数,要根据矩阵短边的长度来确定,也就是如果矩阵的列数比行数少,就要根据列数去求,反之亦然。
其实这里可以看做求对脚线
这里写图片描述

对角线的长度的一半就是圈数。

这里写图片描述
上图这种情况圈数也是2;

当较短的边是奇数的时候:
这里写图片描述
这个时候的,(3-1)/2+1 = 2

在取得圈数的基础上,只要每次输入一圈就能够顺时针遍历完。
接下来要思考的就是如何控制边界。
(1) 从左往右:从左往右的时候,行是固定的,列依次增加1,也就是array[固定][依次+1];
那么行就是第几圈,第一圈的时候,array[0][依次+1];设当前的圈数是circle,二维数组的第二个参数为columns - 0 ,第二圈时为columns - 1,依次类推;
(2) 从上往下:此时列是固定的,为columns - circle - 1;而行是从上往下依次+1的。边界是:行数 - circle。
(3) 从右往左:此时行是固定的,为rows - circle - 1。只需要依次列依次-1,边界条件是:列数 >= 当前的圈数。并且行数 != 当前的圈数,这是对应着特殊情况,如[1,2,3,4,5];
(4)从下往上:此时列是固定的,为circle,行依次-1。

综上分析,代码如下:

import java.util.ArrayList;public class Solution {    public ArrayList<Integer> printMatrix(int [][] array) {        if(array == null) return null;        ArrayList <Integer> result = new ArrayList();        int columns = array[0].length;        int rows = array.length;        // 求圈数        int countOfCircle = 0;         if(columns < rows){            countOfCircle = (columns - 1) / 2 + 1;        }else{            countOfCircle = (rows - 1) / 2 + 1;         }        // 按顺序进行遍历操作,并加入到新的数组中        for(int circle = 0; circle < countOfCircle; circle ++){            // 从左到右            for(int first = circle; first < columns - circle; first ++){                result.add(array[circle][first]);            }            // 从上到下            for(int second = circle + 1; second < rows - circle;second ++){                result.add(array[second][columns - circle -1]);            }            // 从右到左            for(int third = columns - circle - 2; third >= circle && (rows - circle - 1) != circle; third--){                result.add(array[rows - circle - 1][third]);            }            // 从下到上            for(int fourth = rows - circle - 2; fourth > circle && (columns - circle - 1)!= circle;fourth --){                result.add(array[fourth][circle]);            }        }        return result;    }}
0 0
原创粉丝点击