mirror of
https://gitcode.com/Zengtudor/alg2025.git
synced 2025-09-04 01:01:43 +00:00
40 lines
902 B
C++
40 lines
902 B
C++
#include <vector>
|
|
using namespace std;
|
|
|
|
class Solution {
|
|
public:
|
|
vector<int> findDiagonalOrder(const vector<vector<int>>& m) {
|
|
int x=0,y=0;
|
|
bool isup=true;
|
|
vector<int> res;
|
|
res.reserve(m.size());
|
|
#define isout (x<0||x>m.size()||y<0||y>m[0].size())
|
|
do{
|
|
res.push_back(m[x][y]);
|
|
if(isup){
|
|
if(x==0){
|
|
++y;
|
|
isup=false;
|
|
}else{
|
|
--x;
|
|
++y;
|
|
}
|
|
}else{
|
|
if(y==0){
|
|
++x;
|
|
isup=true;
|
|
}else{
|
|
++x;
|
|
--y;
|
|
}
|
|
}
|
|
}while(!(x==m.size()&&y==m[0].size()));
|
|
return res;
|
|
}
|
|
};
|
|
|
|
int main(){
|
|
|
|
Solution s;
|
|
s.findDiagonalOrder({{1,2,3},{4,5,6},{7,8,9}});
|
|
} |