编程语言
首页 > 编程语言> > LeetCode题解(1329):将矩阵按对角线排序(Python)

LeetCode题解(1329):将矩阵按对角线排序(Python)

作者:互联网

题目:原题链接(中等)

标签:数组、排序

解法时间复杂度空间复杂度执行用时
Ans 1 (Python) O ( M × N × l o g ( m i n ( M , N ) ) ) O(M×N×log(min(M,N))) O(M×N×log(min(M,N))) O ( m i n ( M , N ) ) O(min(M,N)) O(min(M,N))44ms (97.99%)
Ans 2 (Python)
Ans 3 (Python)

解法一:

class Solution:
    def diagonalSort(self, mat: List[List[int]]) -> List[List[int]]:
        m, n = len(mat), len(mat[0])

        for d in range(-m + 1, n):
            if d < 0:
                i0, j0, num = -d, 0, min(m + d, n)
            else:
                i0, j0, num = 0, d, min(n - d, m)

            lst = []
            for k in range(num):
                i1, j1 = i0 + k, j0 + k
                lst.append(mat[i1][j1])
            lst.sort()
            for k in range(num):
                i1, j1 = i0 + k, j0 + k
                mat[i1][j1] = lst[k]

        return mat

标签:mat,min,Python,题解,1329,j1,lst,num
来源: https://blog.csdn.net/Changxing_J/article/details/112859029