顺时针打印printMatrix

时间:2019-05-15
本文章向大家介绍顺时针打印printMatrix,主要包括顺时针打印printMatrix使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

题目:顺时针打印矩阵

要求:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下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         //判断数组是否为空
 5         if(matrix==null || matrix.length ==0 || (matrix.length==1 && matrix[0].length ==0))
 6             return null;
 7         //定义列和行,方便以后的代码书写;定义要返回的ArrayList
 8         int row = matrix.length;
 9         int col = matrix[0].length;
10         ArrayList<Integer> result = new ArrayList<Integer>();
11         //开始循环
12         int left=0; 
13         int right= col-1;
14         int top=0;
15         int bottom = row-1;
16         while(left<=right && top<=bottom){
17             for(int i=left;i<=right;++i) result.add(matrix[top][i]);     //从左到右循环   
18             //下面这一行i=top+1,容易出错
19             for(int i=top+1;i<=bottom;++i) result.add(matrix[i][right]);   //从上到下循环
20             if(top!=bottom) {
21                 //下面这一行i=right-1,容易出错
22                 for(int i=right-1;i>=left;--i) result.add(matrix[bottom][i]);   //从右到左循环
23             }
24             if(left!=right){
25                 //下面这一行i=bottom-1,容易出错;而且是i>top,没有=
26                 for(int i=bottom-1;i>top;--i) result.add(matrix[i][left]);   //从下到上循环
27             } 
28             left++; right--; top++; bottom--;
29         }
30         return result;
31     }
32 }

本地编译器上的代码

 1 import java.util.ArrayList;
 2 import java.util.Scanner;
 3 public class Solution515 {
 4     public static ArrayList<Integer> printMatrix(int [][] matrix) {
 5         //判断数组是否为空
 6         if(matrix==null || matrix.length ==0 || (matrix.length==1 && matrix[0].length ==0))
 7             return null;
 8         //定义列和行,方便以后的代码书写;定义要返回的ArrayList
 9         int row = matrix.length;
10         int col = matrix[0].length;
11         ArrayList<Integer> result = new ArrayList<Integer>();
12         //开始循环
13         int left=0; 
14         int right= col-1;
15         int top=0;
16         int bottom = row-1;
17         while(left<=right && top<=bottom){
18             for(int i=left;i<=right;++i) result.add(matrix[top][i]);     //从左到右循环   
19             //下面这一行i=top+1,容易出错
20             for(int i=top+1;i<=bottom;++i) result.add(matrix[i][right]);   //从上到下循环
21             if(top!=bottom) {
22                 //下面这一行i=right-1,容易出错
23                 for(int i=right-1;i>=left;--i) result.add(matrix[bottom][i]);   //从右到左循环
24             }
25             if(left!=right){
26                 //下面这一行i=bottom-1,容易出错;而且是i>top,没有=
27                 for(int i=bottom-1;i>top;--i) result.add(matrix[i][left]);   //从下到上循环
28             } 
29             left++; right--; top++; bottom--;
30         }
31         return result;
32     }
33     public static void main(String [] args) {
34         Scanner sc = new Scanner(System.in );
35         System.out.println("输入二维数组的行数");
36         int row = sc.nextInt();
37         System.out.println("输入二维数组的列数");
38         int col = sc.nextInt();
39         int [][] array = new int[row][col];
40         System.out.println("输入数组各个元素");
41         for(int i=0;i<row;i++){
42             for(int j=0;j<col;j++)
43                 array[i][j]= sc.nextInt();
44         }
45         sc.close();
46         ArrayList<Integer> temp = new ArrayList<Integer>();
47         temp= printMatrix(array);
48         for(int m=0;m<temp.size();m++)
49             System.out.print(temp.get(m)+"  ");
50     }
51 }
View Code

原文地址:https://www.cnblogs.com/shareidea94/p/10872079.html