867. Transpose Matrix

2023-12-21 16:09
文章标签 matrix transpose 867

本文主要是介绍867. Transpose Matrix,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

867. 转置矩阵

给定一个矩阵 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. 1 <= A.length <= 1000
  2. 1 <= A[0].length <= 1000

解法一

//时产复杂度O(n*m), 空间复杂度O(n*m)
class Solution {
public:vector<vector<int>> transpose(vector<vector<int>>& A) {int m = A.size(), n = A[0].size();vector<vector<int>> res(n, vector<int>(m));for(int i = 0; i < m; i++) {for(int j = 0; j < n; j++) {res[j][i] = A[i][j];       }}return res;}
};

先创建一个转置大小的新矩阵,然后遍历原矩阵,填充新矩阵,返回。

2019/07/17 19:47

这篇关于867. Transpose Matrix的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/520632

相关文章

[论文笔记]LLM.int8(): 8-bit Matrix Multiplication for Transformers at Scale

引言 今天带来第一篇量化论文LLM.int8(): 8-bit Matrix Multiplication for Transformers at Scale笔记。 为了简单,下文中以翻译的口吻记录,比如替换"作者"为"我们"。 大语言模型已被广泛采用,但推理时需要大量的GPU内存。我们开发了一种Int8矩阵乘法的过程,用于Transformer中的前馈和注意力投影层,这可以将推理所需

73. Set Matrix Zeros

题目: 解答: 提供了两种解题思路: 第一种,使用两个数组,分别标记每一行、每一列是否有0的存在,然后再去更新二维数组。 第二种,使用两个变量brow,bcol分别标记第0行,第0列是否存在0,然后使用每一行、每一列的第一个单元存储是否该行、该列存在0. 代码: class Solution {public:// 方法一void setZeroes(vector<vector<i

Error: label vector and instance matrix must be double的解决方法

在使用uci下载的数据时,建模时出现这个错误的解决方法 首先现在UCI上面下载数据 然后右键另存为就行了。这样我们就从UCI里面下载到了训练数据 在matlab 点 导入数据,数据类型要记得选第二个, 如果选择最后一个table就会出现这个问题 最后附上代码 %%之前先import wine.date IMPORTED DATA 设为Numeric Matrix (数值矩

python 实现matrix exponentiation矩阵求幂算法

matrix exponentiation矩阵求幂算法介绍 矩阵求幂算法(Matrix Exponentiation)是一种通过利用矩阵乘法的结合律来高效地计算矩阵的幂的算法。这种方法特别适用于在算法竞赛和计算机科学领域中解决需要快速计算矩阵幂的问题,如求解线性递推关系、图论中的路径计数等。 基本思想 矩阵求幂算法的基本思想类似于整数快速幂算法(快速幂算法),通过递归或迭代的方式将矩阵幂的计

[LeetCode] 240. Search a 2D Matrix II

题:https://leetcode.com/problems/search-a-2d-matrix-ii/description/ 题目 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers i

[LeetCode] 566. Reshape the Matrix

题:https://leetcode.com/problems/reshape-the-matrix/description/ 题目 In MATLAB, there is a very useful function called ‘reshape’, which can reshape a matrix into a new one with different size but keep

UVa 11992 Fast Matrix Operations 线段树

UVa 11992 Fast Matrix Operations 题目大意:有一个r行c列的全0矩阵,支持三种操作: 1 x1 y1 x2 y2 v 子矩阵(x1,y1,x2,y2)的所有元素增加v(v > 0)。 2 x1 y1 x2 y2 v 子矩阵(x1,y1,x2,y2)的所有元素设为v(v > 0)。 3 x1 y1 x2 y2    查询子矩阵(x1,y1,x2,y2

【HDU】4965 Fast Matrix Calculation 矩阵快速幂

传送门:【HDU】4965 Fast Matrix Calculation 题目分析:因为比赛的时候写的太匆忙。。写的不堪入目,所以赛后重写了一次,顺便就贴一下了。 因为A*B=C,所以C^(N*N-1) = A*B*A*B*A*...*B*A*B,因为满足结合律所以变成A*( (B*A)^(N*N-2) )*B,因为中间得到的矩阵最大不超过K(K<=6),所以可以对中间的矩阵快速幂,然

leetcode解题思路分析(一百零一)867 - 873 题

转置矩阵 给你一个二维整数数组 matrix, 返回 matrix 的 转置矩阵 。矩阵的 转置 是指将矩阵的主对角线翻转,交换矩阵的行索引与列索引。 加粗样式 class Solution {public:vector<vector<int>> transpose(vector<vector<int>>& matrix) {int m = matrix.size(), n = matrix

01 Matrix

Input:[[0,0,0],[0,1,0],[1,1,1]]Output:[[0,0,0],[0,1,0],[1,2,1]] Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell. The distance between two adjacent cells is 1.