【Golang语言版本】LeetCode 867. Transpose Matrix 矩阵转置

矩阵转置,A[i][j] 变成A[j][i] 比较简单,直接上代码了。

func transpose(A [][]int) [][]int {
    B := make([][]int, len(A[0]))
    for i := 0; i < len(A[0]); i++ {
        B[i] = make([]int, len(A))
        for j := 0; j < len(A); j++ {
            B[i][j] = A[j][i]
        }
    }
    return B
}

原文地址:https://blog.51cto.com/fulin0532/2432313

时间: 2024-10-07 14:51:13

【Golang语言版本】LeetCode 867. Transpose Matrix 矩阵转置的相关文章

LeetCode 867 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. 题目分析及思路 题目要求得到矩阵的转置矩阵.可先得到一个行数与原矩阵列数相等.列数与原矩阵行数相等的矩阵,再对原矩阵进行遍历. python代码? c

867. Transpose Matrix - LeetCode

Question 867. Transpose Matrix Solution 题目大意:矩阵的转置 思路:定义一个转置后的二维数组,遍历原数组,在赋值时行号列号互换即可 Java实现: public int[][] transpose(int[][] A) { int[][] B = new int[A[0].length][A.length]; for (int i = 0; i < A.length; i++) { for (int j = 0; j < A[0].length; j++

【Leetcode_easy】867. Transpose Matrix

problem 867. Transpose Matrix 参考1. Leetcode_easy_867. Transpose Matrix; 完 原文地址:https://www.cnblogs.com/happyamyhope/p/11215040.html

[LeetCode] Reshape the Matrix 矩阵重塑

In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new one with different size but keep its original data. You're given a matrix represented by a two-dimensional array, and two positive integers r and c rep

LeetCode题解Transpose Matrix

1.题目描述 2.题目描述 直接申请内存,转置即可. 3.代码 1 vector<vector<int>> transpose(vector<vector<int>>& A) { 2 int C = A.size() ; 3 int R = A[0].size() ; 4 vector<vector<int>> result(R,vector<int>(C) ) ; 5 6 for( int i = 0 ; i &

【Golang语言】LeetCode 1002. Find Common Characters

Given an array A of strings made only from lowercase letters, return a list of all characters that show up in all strings within the list (including duplicates). For example, if a character occurs 3 times in all strings but not 4 times, you need to i

LeetCode:Spiral Matrix II - 将元素1-n^2以螺旋序填充到矩阵

1.题目名称 Spiral Matrix(螺旋输出矩阵中的元素) 2.题目地址 https://leetcode.com/problems/spiral-matrix-ii/ 3.题目内容 英文:Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. 中文:给出一个整数n,生成一个矩阵,使用数字1到n^2以螺旋顺序填充这个矩阵 例如:给出n=3,则生成如下矩阵:

LeetCode:Spiral Matrix - 螺旋输出矩阵中的元素

1.题目名称 Spiral Matrix(螺旋输出矩阵中的元素) 2.题目地址 https://leetcode.com/problems/spiral-matrix/ 3.题目内容 英文:Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. 中文:给出一个m行n列的矩阵,以螺旋顺序返回矩阵中的所有元素. 例如:现有矩阵如下: [  [ 1,

LeetCode:Set Matrix Zeroes - 将矩阵内含有零的行和列全部置零

1.题目名称 Set Matrix Zeroes(将矩阵内含有零的行和列全部置零) 2.题目地址 https://leetcode.com/problems/set-matrix-zeroes/ 3.题目内容 英文:Given a m x n matrix, if an element is 0, set its entire row and column to 0. 中文:给定一个m×n的矩阵,如果其中一个元素是0,则将该元素所在的整行或整理全部置为0 4.解题方法1 使用第一行和第一列记录某