mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-08-01 00:21:48 +08:00
Add solution 1329
This commit is contained in:
@ -0,0 +1,24 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"sort"
|
||||
)
|
||||
|
||||
func diagonalSort(mat [][]int) [][]int {
|
||||
m, n, diagonalsMap := len(mat), len(mat[0]), make(map[int][]int)
|
||||
for i := 0; i < m; i++ {
|
||||
for j := 0; j < n; j++ {
|
||||
diagonalsMap[i-j] = append(diagonalsMap[i-j], mat[i][j])
|
||||
}
|
||||
}
|
||||
for _, v := range diagonalsMap {
|
||||
sort.Ints(v)
|
||||
}
|
||||
for i := 0; i < m; i++ {
|
||||
for j := 0; j < n; j++ {
|
||||
mat[i][j] = diagonalsMap[i-j][0]
|
||||
diagonalsMap[i-j] = diagonalsMap[i-j][1:]
|
||||
}
|
||||
}
|
||||
return mat
|
||||
}
|
Reference in New Issue
Block a user