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++) {
            B[j][i] = A[i][j];
        }
    }
    return B;
}

原文地址:https://www.cnblogs.com/okokabcd/p/9463056.html

时间: 2024-08-30 14:54:16

867. Transpose Matrix - LeetCode的相关文章

【Leetcode_easy】867. Transpose Matrix

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

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

【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.

Spiral Matrix leetcode java

题目: Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. 题解: 这道题是实现题. 考虑2个初始

Search a 2D Matrix leetcode java

题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previou

leetcode 867. 转置矩阵(Transpose Matrix)

目录 题目描述: 示例 1: 示例 2: 解法: 题目描述: 给定一个矩阵 A, 返回 A 的转置矩阵. 矩阵的转置是指将矩阵的主对角线翻转,交换矩阵的行索引与列索引. 示例 1: 输入:[[1,2,3],[4,5,6],[7,8,9]] 输出:[[1,4,7],[2,5,8],[3,6,9]] 示例 2: 输入:[[1,2,3],[4,5,6]] 输出:[[1,4],[2,5],[3,6]] 提示: 1 <= A.length <= 1000 1 <= A[0].length <

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 &

Toeplitz Matrix - LeetCode

LeetCode 题目地址 描述 A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element. Now given an M x N matrix, return True if and only if the matrix is Toeplitz. Example 1: Input: matrix = [[1,2,3,4],[5,1,2,3],[9,5,1,2]] Outpu

Search a 2D Matrix leetcode

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous ro