解题思路:
矩阵的大小为m*n,对角线个数为m+n-1,所以只要遍历m+n-1次即可,对于每一次遍历,要判断从下往上还是从上往下,在每个判断中,还有一个判断是在中心对角线上面还是下面,模拟好每种情况即可,代码如下:
class Solution {
public:
vector findDiagonalOrder(vector>& mat) {
int m = mat.size(), n = mat[0].size();
vector res;
int index = 0;
for(int i = 0; i < m + n - 1; i ++) {
if(i % 2) {
int x = i < n ? 0 : i - n + 1;
int y = i < n ? i : n - 1;
while(x < m && y >= 0) {
res.emplace_back(mat[x][y]);
x ++;
y --;
}
} else {
int x = i < m ? i : m - 1;
int y = i < m ? 0 : i - m + 1;
while(x >= 0 && y < n) {
res.emplace_back(mat[x][y]);
x --;
y ++;
}
}
}
return res;
}
};


![LeetCode498 对角线遍历[模拟] HERODING的LeetCode之路 LeetCode498 对角线遍历[模拟] HERODING的LeetCode之路](http://www.mshxw.com/aiimages/31/971192.png)
