Skip to content

Commit

Permalink
对角线遍历
Browse files Browse the repository at this point in the history
  • Loading branch information
eyeeco committed Feb 15, 2019
1 parent 445d7c0 commit 4f2b4c8
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Leetcode_Part2/498. 对角线遍历.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public:
vector<int> findDiagonalOrder(vector<vector<int>>& matrix) {
if(matrix.size()==0) return vector<int>();
int m = matrix.size(), n = matrix[0].size();
vector<list<int>> con(m+n);
vector<int> res;
for(int i=0; i<matrix.size(); i++){
for(int j=0; j<matrix[0].size(); j++){
int t = i + j;
if(t % 2 == 0) con[t].push_front(matrix[i][j]);
if(t % 2 == 1) con[t].push_back(matrix[i][j]);
}
}
for(auto buk:con){
for(int num:buk){
res.push_back(num);
}
}
return res;
}
};

0 comments on commit 4f2b4c8

Please sign in to comment.