转置矩阵

题目描述:

给定一个矩阵 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]] 解题:
尺寸为 R x C 的矩阵 A 转置后会得到尺寸为 C x R 的矩阵 ans,对此有 ans[c][r] = A[r][c]。
class Solution {
public int[][] transpose(int[][] A) {
int row = A.length;
int col = A[0].length;
int[][] arr = new int[col][row]; for(int r=0; r<row; r++){
for(int c=0; c<col; c++){
arr[c][r] = A[r][c]; //行变列,列变行
}
}
return arr;
}
}

复杂度分析

  • 时间复杂度:O(R∗C),其中 R 和 C 是给定矩阵 A 的行数和列数。

  • 空间复杂度:O(R∗C),也就是答案所使用的空间。

leetcode之转置矩阵的更多相关文章

  1. 领扣(LeetCode)转置矩阵 个人题解

    给定一个矩阵 A, 返回 A 的转置矩阵. 矩阵的转置是指将矩阵的主对角线翻转,交换矩阵的行索引与列索引. 示例 1: 输入:[[1,2,3],[4,5,6],[7,8,9]] 输出:[[1,4,7] ...

  2. LeetCode 867. 转置矩阵

    题目链接:https://leetcode-cn.com/problems/transpose-matrix/ 给定一个矩阵 A, 返回 A 的转置矩阵. 矩阵的转置是指将矩阵的主对角线翻转,交换矩阵 ...

  3. [LeetCode] Transpose Matrix 转置矩阵

    Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix flipped over it ...

  4. Leetcode#867. Transpose Matrix(转置矩阵)

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

  5. [LeetCode] Rotate Image 旋转图像

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  6. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  7. [Swift]LeetCode867. 转置矩阵 | Transpose Matrix

    Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix flipped over it ...

  8. LeetCode(867)

    title: LeetCode(867) tags: Python Algorithm 题目描述 给定一个矩阵 A, 返回 A 的转置矩阵. 矩阵的转置是指将矩阵的主对角线翻转,交换矩阵的行索引与列索 ...

  9. 前端与算法 leetcode 48. 旋转图像

    目录 # 前端与算法 leetcode 48. 旋转图像 题目描述 概要 提示 解析 解法一:转置加翻转 解法二:在单次循环中旋转 4 个矩形 算法 传入测试用例的运行结果 执行结果 GitHub仓库 ...

随机推荐

  1. Pig store用法举例

    store:将数据存储到HDFS等文件系统里   将数据保存到/data目录 store data into '/data'; 以逗号为分隔符 store data into '/data' usin ...

  2. Pig foreach用法举例

    foreach:一行一行的遍历数据,处理一行的数据,然后返回一个tuple. users = load '/users.data';   1)别名引用 f = foreach users genera ...

  3. npm私有仓库搭建

    背景 Node.js开发本地项目,有时不同项目之间存在依赖,如果不想把项目发布到npm社区的仓库,则需要有自己本地的仓库. 有些公司采用的是内网开发,很多npm资源无法从内网去下载. sinopia( ...

  4. webservice双向验证

    ServicePointManager.Expect100Continue = true; ServicePointManager.SecurityProtocol = SecurityProtoco ...

  5. 用Model来计算cell的高度

    用Model来计算cell的高度 效果: 将计算cell高度的方法直接移植到Model当中,初始化的瞬间就计算好了高度,非常好用! 源码: Model // // Model.h // // Copy ...

  6. [翻译] DraggableYoutubeFloatingVideo

    DraggableYoutubeFloatingVideo DraggableYoutubeFloatingVideo allows you to play videos on a floating ...

  7. Docker 命令总结

    1 启动镜像 docker run -i -t centos /bin/bash

  8. 【模块化】 RequireJS入门教程总结与推荐

    之所以学习RequireJS,肯定对 模块化有一定的理解.这里有几篇学习 RequireJS的文章,推荐给大家去学习. Javascript模块化编程(一):模块的写法 Javascript模块化编程 ...

  9. Winform启动时隐藏不显示

    我最终用了这个方法:1.MainForm的构造方法中添加: public MainForm() { InitializeComponent(); this.ShowInTaskbar = false; ...

  10. 最新版本2018.1.1webstorm安装、汉化、破解教程

    一.安装(下载与激活) 1.官网下载安装包https://www.jetbrains.com/webstorm/ 2.开始安装 3.选择安装目录,点击下一步 4.勾选64位,点击下一步 5.继续下一步 ...