diff --git a/leetcode/0048.Rotate-Image/48. Rotate Image.go b/leetcode/0048.Rotate-Image/48. Rotate Image.go index a21d5fa8..9b37fa61 100644 --- a/leetcode/0048.Rotate-Image/48. Rotate Image.go +++ b/leetcode/0048.Rotate-Image/48. Rotate Image.go @@ -1,26 +1,17 @@ package leetcode func rotate(matrix [][]int) { - row := len(matrix) - if row <= 0 { - return - } - column := len(matrix[0]) + length := len(matrix) // rotate by diagonal 对角线变换 - for i := 0; i < row; i++ { - for j := i + 1; j < column; j++ { - tmp := matrix[i][j] - matrix[i][j] = matrix[j][i] - matrix[j][i] = tmp + for i := 0; i < length; i++ { + for j := i + 1; j < length; j++ { + matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j] } } // rotate by vertical centerline 竖直轴对称翻转 - halfColumn := column / 2 - for i := 0; i < row; i++ { - for j := 0; j < halfColumn; j++ { - tmp := matrix[i][j] - matrix[i][j] = matrix[i][column-j-1] - matrix[i][column-j-1] = tmp + for i := 0; i < length; i++ { + for j := 0; j < length/2; j++ { + matrix[i][j], matrix[i][length-j-1] = matrix[i][length-j-1], matrix[i][j] } } } diff --git a/website/content/ChapterFour/0001~0099/0048.Rotate-Image.md b/website/content/ChapterFour/0001~0099/0048.Rotate-Image.md index 3354ad47..ed14174c 100755 --- a/website/content/ChapterFour/0001~0099/0048.Rotate-Image.md +++ b/website/content/ChapterFour/0001~0099/0048.Rotate-Image.md @@ -97,35 +97,23 @@ You have to rotate the image **[in-place](https://en.wikipedia.org/wiki/In-plac ## 代码 ```go - package leetcode func rotate(matrix [][]int) { - row := len(matrix) - if row <= 0 { - return - } - column := len(matrix[0]) + length := len(matrix) // rotate by diagonal 对角线变换 - for i := 0; i < row; i++ { - for j := i + 1; j < column; j++ { - tmp := matrix[i][j] - matrix[i][j] = matrix[j][i] - matrix[j][i] = tmp + for i := 0; i < length; i++ { + for j := i + 1; j < length; j++ { + matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j] } } // rotate by vertical centerline 竖直轴对称翻转 - halfColumn := column / 2 - for i := 0; i < row; i++ { - for j := 0; j < halfColumn; j++ { - tmp := matrix[i][j] - matrix[i][j] = matrix[i][column-j-1] - matrix[i][column-j-1] = tmp + for i := 0; i < length; i++ { + for j := 0; j < length/2; j++ { + matrix[i][j], matrix[i][length-j-1] = matrix[i][length-j-1], matrix[i][j] } } } - - ```