yankewei / LeetCode

LeetCode 问题的解决方法
MIT License
6 stars 0 forks source link

转置矩阵 #117

Open yankewei opened 3 years ago

yankewei commented 3 years ago

给你一个二维整数数组 matrix, 返回 matrix 的 转置矩阵 。

矩阵的 转置 是指将矩阵的主对角线翻转,交换矩阵的行索引与列索引。

示例 1:

输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
输出:[[1,4,7],[2,5,8],[3,6,9]]

示例 2:

输入:matrix = [[1,2,3],[4,5,6]]
输出:[[1,4],[2,5],[3,6]]

提示:

来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/transpose-matrix 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

yankewei commented 3 years ago

没什么说的

func transpose(matrix [][]int) [][]int {
    row, column := len(matrix), len(matrix[0])
    ret := make([][]int, column)
    for i := 0; i < column; i++ {
        t := make([]int, row)
        for j := 0; j < row; j++ {
            t[j] = matrix[j][i]
        }
        ret[i] = t
    }
    return ret
}