Sort the Matrix Diagonally
给一个2d整数数组, 求按照对角线的数字排序后的数组. 这个就按照题意写就好了.
class Solution {
public int[][] diagonalSort(int[][] mat) {
int n = mat.length;
int m = mat[0].length;
int i = n - 1;
int j = 0;
while(i >= 0) {
int k = i;
int q = j;
List<Integer> list = new ArrayList<>();
while(k <= n - 1 && q <= m - 1) {
list.add(mat[k++][q++]);
}
Collections.sort(list);
while(k <= n - 1 && q <= m - 1) {
list.add(mat[k++][q++]);
}
k = i;
q = j;
int p = 0;
while(k <= n - 1 && q <= m - 1) {
mat[k++][q++] = list.get(p++);
}
i--;
}
i = 0;
j = 0;
while(j <= m - 1) {
int k = i;
int q = j;
List<Integer> list = new ArrayList<>();
while(k <= n - 1 && q <= m - 1) {
list.add(mat[k++][q++]);
}
Collections.sort(list);
while(k <= n - 1 && q <= m - 1) {
list.add(mat[k++][q++]);
}
k = i;
q = j;
int p = 0;
while(k <= n - 1 && q <= m - 1) {
mat[k++][q++] = list.get(p++);
}
j++;
}
return mat;
}
}