我是靠谱客的博主 飘逸咖啡,这篇文章主要介绍顺时针打印矩阵(牛客网十一),现在分享给大家,希望可以做个参考。

输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 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
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import java.util.ArrayList; public class Solution { public ArrayList<Integer> printMatrix(int [][] matrix) { ArrayList result=new ArrayList(); if(matrix.length==0) return result; int n1=0; int n2=matrix.length-1; int nn=n2+1; int m1=0; int m2=matrix[0].length-1; int mm=m2+1; int count=0; while(count<nn*mm) { if(m1<=m2) { for(int j=m1;j<=m2;j++) { result.add(matrix[n1][j]); count++; } n1++; } if(n1<n2 && count<nn*mm) { for(int i=n1;i<=n2;i++) { result.add(matrix[i][m2]); count++; } m2--; } if(m1<m2 && count<nn*mm) { for(int jj=m2;jj>=m1;jj--) { result.add(matrix[n2][jj]); count++; } n2--; } if(n1<n2 && count<nn*mm) { for(int ii=n2;ii>=n1;ii--) { result.add(matrix[ii][m1]); count++; } m1++; } } return result; } } 判断条件过多,这边有时间重新整理。




最后

以上就是飘逸咖啡最近收集整理的关于顺时针打印矩阵(牛客网十一)的全部内容,更多相关顺时针打印矩阵(牛客网十一)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(66)

评论列表共有 0 条评论

立即
投稿
返回
顶部