文章目录
  1. 1. 题目
  2. 2. 解答
  3. 3. 代码
  4. 4. 参考

题目

给你一个大于0的整数n,请输出一个n行n列的数据表,并且要满足以下规律(以n=10为例)

n行n列的数据表

解答

  1. 按圈遍历数组
    函数:int[][] genCycleMatrix(int rows, int columns)

    • 数字是按顺时针一圈一圈递增的,从1 开始 ;
    • 每一圈数字的开头都是方阵[n×n]对角线上的数字([0,0],[1,1]…),n = min(rows, columns),其中rows, columns分别为行数和列数
    • 一般情况下,一圈有两行两列,当行数或列数为奇数时,最后一圈会出现少行少列现象。故按圈打印数字的终止条件是start * 2 <n
      代码如下:
      1
      2
      3
      4
      int startNumber = 1;
      for (int start = 0; start * 2 < rows && start *2 < columns ;start++) {
      startNumber = genCycleMatrix(a, rows, columns, start, startNumber);
      }
  2. 遍历一圈数组,为依次遍历的元素赋值,并返回下一圈应该开始的数字
    函数:int genCycleMatrix(int[][] a, int rows, int columns , int start ,int startNumber)
    依次分四个方向从左到右从上到下从右到左从下到上
    相对于剑指Offer中花大篇幅讲的判断条件,其实可以先写出循环代码,再写满足循环的判断条件,这样就简单许多。
    需要注意的是后两个方向还需要添加条件start < endX .

    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
    public static int genCycleMatrix(int[][] a, int rows, int columns 
    , int start ,int startNumber){
    int endX = rows -1 - start;
    int endY = columns -1 - start;

    //从左到右
    for (int i = start; i <= endX; i++) {
    a[start][i] = startNumber++;
    }
    //从上到下
    if (start < endY) {
    for (int i = start+1; i <= endY; i++) {
    a[i][endY] = startNumber++;
    }
    }
    //从右到左
    if (start < endY && start < endX) {
    for (int i = endX-1; i >= start; i--) {
    a[endY][i] = startNumber++;
    }
    }
    //从下到上
    if (start < endY-1 && start < endX) {
    for (int i = endY-1; i > start; i--) {
    a[i][start] = startNumber++;
    }
    }

    return startNumber;
    }
  3. 打印数组,用于验证生成的数组的正确性
    函数:void print2DimMatrix(int[][] a ,int rows, int columns) throws Exception
    这段代码比较简单,就是打印一个2维数组。见 #代码

代码

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
public class MatrixInCycle {

public static int[][] genCycleMatrix(int rows, int columns)
throws Exception{
int[][] a = new int[rows][columns];

int startNumber = 1;
for (int start = 0; start * 2 < rows && start *2 < columns ;start++) {
startNumber = genCycleMatrix(a, rows, columns, start, startNumber);
}

return a;

}

public static int genCycleMatrix(int[][] a, int rows, int columns
, int start ,int startNumber){
int endX = rows -1 - start;
int endY = columns -1 - start;

//从左到右
for (int i = start; i <= endX; i++) {
a[start][i] = startNumber++;
}
//从上到下
if (start < endY) {
for (int i = start+1; i <= endY; i++) {
a[i][endY] = startNumber++;
}
}
//从右到左
if (start < endY && start < endX) {
for (int i = endX-1; i >= start; i--) {
a[endY][i] = startNumber++;
}
}
//从下到上
if (start < endY-1 && start < endX) {
for (int i = endY-1; i > start; i--) {
a[i][start] = startNumber++;
}
}

return startNumber;
}

public static void print2DimMatrix(int[][] a ,int rows, int columns)
throws Exception{
if (a ==null ) {
throw new Exception("array can not be null!");
}

for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.printf("%8d",a[i][j]);
}
System.out.println();
}
}

public static void main(String[] args) throws Exception {
int[][] a= genCycleMatrix(6, 6);
print2DimMatrix(a,6, 6);
}
}

参考

[1]:剑指Office-面试题20:顺时针打印矩阵
[2]:月初新浪的一道关于算法的笔试题

文章目录
  1. 1. 题目
  2. 2. 解答
  3. 3. 代码
  4. 4. 参考