1329. 将矩阵按对角线排序
题目链接:1329. 将矩阵按对角线排序
代码如下:
cpp
//参考:https://leetcode.cn/problems/sort-the-matrix-diagonally/solutions/2760094/dui-jiao-xian-pai-xu-fu-yuan-di-pai-xu-p-uts8
class Solution {
public:
vector<vector<int>> diagonalSort(vector<vector<int>>& mat)
{
int m=mat.size(),n=mat[0].size();
vector<int> a(min(m,n));
for(int k=1-n;k<m;k++)//k=i-j
{
int left_i=max(k,0),right_i=min(k+n,m);
for(int i=left_i;i<right_i;i++)
{
a[i-left_i]=mat[i][i-k];
}
sort(a.begin(),a.begin()+(right_i-left_i));
for(int i=left_i;i<right_i;i++)
{
mat[i][i-k]=a[i-left_i];
}
}
return mat;
}
};