题目描述

给定一个矩阵 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

思路

新建一个矩阵res,res[i][j]=A[j][i]

代码实现

package Array;

/**
* 867. Transpose Matrix(转置矩阵)
* 给定一个矩阵 A, 返回 A 的转置矩阵。
* 矩阵的转置是指将矩阵的主对角线翻转,交换矩阵的行索引与列索引。
*/
public class Solution867 {
public static void main(String[] args) {
Solution867 solution867 = new Solution867();
int[][] A = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int[][] res = solution867.transpose(A);
for (int i = 0; i < res.length; i++) {
for (int j = 0; j < res[i].length; j++) {
System.out.print(res[i][j] + " ");
}
System.out.println();
}
} public int[][] transpose(int[][] A) {
int col = A.length;
int row = A[0].length;
int[][] res = new int[row][col];
for(int i = 0;i < row;i++){
for(int j = 0;j < col;j++){
res[i][j] = A[j][i];
}
}
return res;
}
}

Leetcode#867. Transpose Matrix(转置矩阵)的更多相关文章

  1. LeetCode 867 Transpose Matrix 解题报告

    题目要求 Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix flipped ov ...

  2. Leetcode 867. Transpose Matrix

    class Solution: def transpose(self, A: List[List[int]]) -> List[List[int]]: return [list(i) for i ...

  3. 【LEETCODE】48、867. Transpose Matrix

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  4. 867. Transpose Matrix - LeetCode

    Question 867. Transpose Matrix Solution 题目大意:矩阵的转置 思路:定义一个转置后的二维数组,遍历原数组,在赋值时行号列号互换即可 Java实现: public ...

  5. 【Leetcode_easy】867. Transpose Matrix

    problem 867. Transpose Matrix solution: class Solution { public: vector<vector<int>> tra ...

  6. [LeetCode] Transpose Matrix 转置矩阵

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

  7. 【LeetCode】867. Transpose Matrix 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先构建数组再遍历实现翻转 日期 题目地址:https ...

  8. [LeetCode&Python] Problem 867. Transpose Matrix

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

  9. 【leetcode】867 - Transpose Matrix

    [题干描述] Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix flipped ...

随机推荐

  1. gradle下载jar包

    使用IntelliJ IDEA+gradle的时候,有时用到的是公司的jar包,如果在外面就无法访问仓库,因此试着下载所需要的jar包到电脑上.然后发现原来gradle已经缓存到本地上了,可用如下方式 ...

  2. padding内边距

    android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/ac ...

  3. Jquery Mobile列表

    向 <ol> 或 <ul> 元素添加 data-role="listview" 1.圆角和外边距 :data-inset="true" ...

  4. python: 基本知识(一)

    从今天开始继续python的学习,将应用到到黑客学习中,一边学习黑客知识一边学习python. 1.类:(封装) class T: def  __init__(self,...): //类对象创建后调 ...

  5. mysql批量插入简单测试数据

    mysql批量插入简单测试数据 # 参考网址: https://www.2cto.com/database/201703/618280.html 1.mysql创建测试表 CREATE TABLE ` ...

  6. go官方的http.request + context样例

    go官方的http.request + context样例 https://github.com/DavadDi/go_study/blob/master/src/httpreq_context/ma ...

  7. 安装saltstack使用的shell

    sed -i 's/^#//g'  /etc/yum.repos.d/centos6.8.repo sed -i 's/enabled=0/enabled=1/g'  /etc/yum.repos.d ...

  8. 【C#】C#格式化文件大小

    /// <summary> /// 格式化文件大小的C#方法 /// </summary> /// <param name="filesize"> ...

  9. Selenium模块的使用

    Selenium是一个Web的自动化测试工具,最初是为网站自动化测试而开发的,类型像我们玩游戏用的按键精灵,可以按指定的命令自动操作,不同是Selenium 可以直接运行在浏览器上,它支持所有主流的浏 ...

  10. js截取url地址后面的文件名

    let url = response.data.stuXscg[0].fj let num = url.lastIndexOf('/')+1 let name = url.substring(num) ...