解题思路:
(1)先找一个顺序,从下往上,从左到右
(2)先排序再赋值
class Solution {
public:
vector<vector<int>> diagonalSort(vector<vector<int>>& mat) {
int row = mat.size()-1;
int col = mat[0].size()-1;
//排序
vector<vector<int>> vec;
for(int i=row;i>=0;i--) {
vector<int> v;
int j=0,temp=i;
while(temp<=row && j<=col) {
v.push_back(mat[temp][j]);
temp++;
j++;
}
sort(v.begin(),v.end());
vec.push_back(v);
}
for(int i=1;i<=col;i++) {
vector<int> v;
int j=0,temp=i;
while(temp<=col && j<=row) {
v.push_back(mat[j][temp]);
temp++;
j++;
}
sort(v.begin(),v.end());
vec.push_back(v);
}
//赋值
for(int i=row;i>=0;i--) {
int j=0,temp=i,count=0;
while(temp<=row && j<=col) {
mat[temp][j] = vec[row-i][count];
temp++;
j++;
count++;
}
}
for(int i=1;i<=col;i++) {
int j=0,temp=i,count=0;
while(temp<=col && j<=row) {
mat[j][temp] = vec[row+i][count];
temp++;
j++;
count++;
}
}
return mat;
}
};