题解 | #二维数组打印#
二维数组打印
http://www.nowcoder.com/practice/6fadc1dac83a443c9434f350a5803b51
解题思路:两层循环,外层循环控制次数,内层循环负责向数组中写入数据
import java.util.*;
public class Printer {
public int[] arrayPrint(int[][] arr, int n) {
// write code here
int[] res = new int[n * n];
int index = 0;
for (int j = n - 1; j >= 0; j--) {
for (int i = 0; i < n - j; i++) {
res[index] = arr[i][j + i];
res[n * n - 1 - index] = arr[n - 1 - i][n - 1 - j - i];
index++;
}
}
return res;
}
}