/* 自定義代碼塊樣式 */

herrDeng網內搜尋

自訂搜尋

Ads

2025年8月28日 星期四

Py3 C++速解Leetcode 3446 Sort Matrix by Diagonals/with Py3 code


Py3 C++速解Leetcode 3446  Sort Matrix by Diagonals
矩陣問題都是很好的迴圈、陣列練習,又加上排序那就是很好的練習題
[Py3 code請進]
-------
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
        
        
        

沒有留言:

Related Posts Plugin for WordPress, Blogger...

熱門文章