19 顺时针打印矩阵printMatrix <输入m*n二维数组>

题目:顺时针打印矩阵

要求:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下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         //开始循环
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             //需要加入条件判断,防止出现单行或者单列的情况。
22             if(top!=bottom) {
23                 //下面这一行i=right-1,容易出错
24                 for(int i=right-1;i>=left;--i) result.add(matrix[bottom][i]);   //从右到左循环
25             }
26             //需要加入条件判断,防止出现单行或者单列的情况。
27             if(left!=right){
28                 //下面这一行i=bottom-1,容易出错;而且是i>top,没有=
29                 for(int i=bottom-1;i>top;--i) result.add(matrix[i][left]);   //从下到上循环
30             } 
31             left++; right--; top++; bottom--;
32         }
33         return result;
34     }
35 }

 

本地编译器上的代码

 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

给定一个二维数组m*n,我们需要按照顺时针方向,从外到内依次写入数字1到m*n,并最后打印出这个二维数组。 首先,我们需要定义四个变量,分别表示数组的行和列的上下边界,初始值为0和n-1(行边界)和0和m-1(列边界)。还需要定义一个变量k,表示要写入的数字,初始值为1。 然后,我们使用一个while循环,判断当上边界小于等于下边界且左边界小于等于右边界的时候,继续进行写入数字的操作。 在每一次循环中,我们首先从左到右遍历上边界,即将matrix[i][j]赋值为k,并将j自增。然后,上边界向下移动一行,即将i自增。接下来,我们从上到下遍历右边界,即将matrix[i][j]赋值为k,并将i自增。然后,右边界向左移动一列,即将j自减。在接下来的两个循环中,我们分别从右到左遍历下边界和从下到上遍历左边界,同样的方式进行赋值和边界移动。 当while循环结束后,我们就完成了1到m*n的顺时针写入操作。最后,我们使用两个for循环打印出数组的内容。 下面是一个示例的代码实现: ```python def printMatrix(m, n): matrix = [[0] * n for _ in range(m)] top = 0 bottom = m - 1 left = 0 right = n - 1 k = 1 while top <= bottom and left <= right: for j in range(left, right + 1): matrix[top][j] = k k += 1 top += 1 for i in range(top, bottom + 1): matrix[i][right] = k k += 1 right -= 1 for j in range(right, left - 1, -1): matrix[bottom][j] = k k += 1 bottom -= 1 for i in range(bottom, top - 1, -1): matrix[i][left] = k k += 1 left += 1 for i in range(m): for j in range(n): print(matrix[i][j], end=' ') print() m = 3 n = 4 printMatrix(m, n) ``` 以上代码将输出以下结果: 1 2 3 4 10 11 12 5 9 8 7 6
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值