要求返回一个m x n的vector<vector<int>>数组,数组中的元素为0至m x n -1,要求沿着斜线排列,如下所示
[0 1 5 6 ]
[2 4 7 10]
[3 8 9 11]
class Solution { public: vector<vector<int> > print_rectangle(int m,int n) { vector<vector<int>> ret(m,vector<int>(n)); int val = 0; int i = 0; int j = 0; int flag = 0; while(0<=i && i<m && 0<=j && j<n) { if(flag == 0) { while(0<=i && j<n) { ret[i][j] = val; val++; i--; j++; } i--; j++; (j == n-1)?i++:j++; } else { while(i<m && 0<=j) { ret[i][j] = val; val++; i++; j--; } i++; j--; (i == m-1)?j++:i++; } flag = 1-flag; } return ret; } };
原文地址:https://www.cnblogs.com/zjuhaohaoxuexi/p/11829127.html
时间: 2024-10-05 05:04:44