mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-07 09:54:57 +08:00
规范格式
This commit is contained in:
14
leetcode/0867.Transpose-Matrix/867. Transpose Matrix.go
Normal file
14
leetcode/0867.Transpose-Matrix/867. Transpose Matrix.go
Normal file
@ -0,0 +1,14 @@
|
||||
package leetcode
|
||||
|
||||
func transpose(A [][]int) [][]int {
|
||||
row, col, result := len(A), len(A[0]), make([][]int, len(A[0]))
|
||||
for i := range result {
|
||||
result[i] = make([]int, row)
|
||||
}
|
||||
for i := 0; i < row; i++ {
|
||||
for j := 0; j < col; j++ {
|
||||
result[j][i] = A[i][j]
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
47
leetcode/0867.Transpose-Matrix/867. Transpose Matrix_test.go
Normal file
47
leetcode/0867.Transpose-Matrix/867. Transpose Matrix_test.go
Normal file
@ -0,0 +1,47 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question867 struct {
|
||||
para867
|
||||
ans867
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para867 struct {
|
||||
A [][]int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans867 struct {
|
||||
B [][]int
|
||||
}
|
||||
|
||||
func Test_Problem867(t *testing.T) {
|
||||
|
||||
qs := []question867{
|
||||
|
||||
question867{
|
||||
para867{[][]int{[]int{1, 2, 3}, []int{4, 5, 6}, []int{7, 8, 9}}},
|
||||
ans867{[][]int{[]int{1, 4, 7}, []int{2, 5, 8}, []int{3, 6, 9}}},
|
||||
},
|
||||
|
||||
question867{
|
||||
para867{[][]int{[]int{1, 2, 3}, []int{4, 5, 6}}},
|
||||
ans867{[][]int{[]int{1, 4}, []int{2, 5}, []int{3, 6}}},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 867------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans867, q.para867
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, transpose(p.A))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
35
leetcode/0867.Transpose-Matrix/README.md
Executable file
35
leetcode/0867.Transpose-Matrix/README.md
Executable file
@ -0,0 +1,35 @@
|
||||
# [867. Transpose Matrix](https://leetcode.com/problems/transpose-matrix/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given a matrix `A`, return the transpose of `A`.
|
||||
|
||||
The transpose of a matrix is the matrix flipped over it's main diagonal, switching the row and column indices of the matrix.
|
||||
|
||||
**Example 1:**
|
||||
|
||||
Input: [[1,2,3],[4,5,6],[7,8,9]]
|
||||
Output: [[1,4,7],[2,5,8],[3,6,9]]
|
||||
|
||||
**Example 2:**
|
||||
|
||||
Input: [[1,2,3],[4,5,6]]
|
||||
Output: [[1,4],[2,5],[3,6]]
|
||||
|
||||
**Note:**
|
||||
|
||||
1. `1 <= A.length <= 1000`
|
||||
2. `1 <= A[0].length <= 1000`
|
||||
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定一个矩阵 A, 返回 A 的转置矩阵。矩阵的转置是指将矩阵的主对角线翻转,交换矩阵的行索引与列索引。
|
||||
|
||||
|
||||
## 解题思路
|
||||
|
||||
|
||||
- 给出一个矩阵,顺时针旋转 90°
|
||||
- 解题思路很简单,直接模拟即可。
|
Reference in New Issue
Block a user