题解 | #牛牛的田地#
牛牛的田地
https://www.nowcoder.com/practice/55bad8d388ee4373a108b15a0b0a3ed7
对角线遍历
#include <vector> class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param mat int整型vector<vector<>> * @return int整型vector */ vector<int> diagonalTraverse(vector<vector<int> >& mat) { int n = mat.size(); int m = mat[0].size(); vector<int> ret; for(int i=0;i<m+n-1;i++){ if(i%2==0){ int r = i < n ? i : n-1; int c = i < n ? 0 : i-n+1; while(r>=0&&r<n&&c>=0&&c<m) ret.push_back(mat[r--][c++]); } else { int r = i < m? 0 : i-m+1; int c = i < m? i : m-1; while(r>=0&&r<n&&c>=0&&c<m) ret.push_back(mat[r++][c--]); } } return ret; } };