LeetCode 每日一练 ---- 1329. Sort Matrix by Diagonals
发布时间
阅读量:
阅读量
LeetCode 每日一题 ---- 【1329.将矩阵按对角线排序】
- 1329.对矩阵元素进行对角线方向的排序处理
-
- 途径:采用模拟的方式实现
-
矩阵对角线排序实现
模拟方法应用
按照每条对角线的顺序进行遍历,将获取到的数据依次存入数组,随后对数组内容进行排序操作,最后再将排序后的元素按顺序回填至原位置即可完成处理。
需要特别注意的是:
i - j + m 这一表达式能够唯一地标识某一条对角线,采用这种方式进行处理将大大提升操作的便捷性。
class Solution {
public int[][] diagonalSort(int[][] mat) {
int n = mat.length, m = mat[0].length;
List<List<Integer>> list = new ArrayList<>(m + n);
for (int i = 0; i < m + n; i ++ ) {
list.add(new ArrayList<>());
}
for (int i = 0; i < n; i ++ ) {
for (int j = 0; j <
全部评论 (0)
还没有任何评论哟~
