leetcode1329

 1 class Solution:
 2     def diagonalSort(self, mat: 'List[List[int]]') -> 'List[List[int]]':
 3         m = len(mat)
 4         n = len(mat[0])
 5 
 6         i,j = m-1,0
 7         while i != 0 or j != n-1:
 8             x,y = i,j
 9             temp = []
10             while x < m and y < n:
11                 temp.append(mat[x][y])
12                 if x < m:
13                     x += 1
14                 if y < n:
15                     y += 1
16             temp.sort()
17             x,y,z = i,j,0
18             while x < m and y < n:
19                 mat[x][y] = temp[z]
20                 if x < m:
21                     x += 1
22                 if y < n:
23                     y += 1
24                 z += 1
25             if i > 0:
26                 i -= 1
27                 j = 0
28             else:
29                 i = 0
30                 if j < n:
31                     j += 1
32         return mat

算法思路:二维数组遍历。

原文地址:https://www.cnblogs.com/asenyang/p/12233716.html