https://warmland.gitbooks.io/algorithm/content/interview/snapchat_-_xiang_zuo_xia_jiao_da_yin_2d_ju_zhen.html
题目是这样的,给一个2d矩阵,向左下角打印矩阵,比如
1,2,3,4
5,6,7,8
9,10,11,12
结果是
{{1},{2,5},{3,6,9},{4,7,10},{8,11},{12}}
所以解题的重点是,结果里面的每一组,它们的横坐标+纵坐标是相等的,然后最大的可能是col+row-2
public List<List<Integer>> printMatrix(int[][] board) {
int height = board.length;
int width = board[0].length;
List<List<Integer>> res = new ArrayList<>();
if (width == 0 || height == 0) {
return res;
}
int diagonal = width + height - 2;
for (int sum = 0; sum <= diagonal; sum++) {
List<Integer> cur = new ArrayList<>();
for (int y = 0; y <= sum; y++) {
int x = sum - y;
if (y < height && x < width) {
//cur.add(y * width + x + 1);
cur.add(board[y][x]);
}
}
res.add(cur);
}
return res;
}