Py3 C++速解Leetcode 3446 Sort Matrix by Diagonals
矩陣問題都是很好的迴圈、陣列練習,又加上排序那就是很好的練習題
-------
Matrix problems are good for looping and array exercises, and adding sorting makes them a good exercise.
class Solution: def sortMatrix(self, grid: List[List[int]]) -> List[List[int]]: n=len(grid) # upper right triangle j=i+d for d in range(n-2, 0, -1): diag=sorted(grid[i][i+d] for i in range(n-d)) for i, x in enumerate(diag): grid[i][i+d]=x # lower left triangle i=j+d for d in range(n-1): diag=sorted((grid[j+d][j] for j in range(n-d)), reverse=True) for j, x in enumerate(diag): grid[j+d][j]=x return grid
沒有留言:
張貼留言