题目描述

给定一个矩阵 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. Django cookie相关操作

    Django cookie 的相关操作还是比较简单的 首先是存储cookie #定义设置cookie(储存) def save_cookie(request): #定义回应 response = Ht ...

  2. Day035--Python--管道, Manager, 进程池, 线程切换

    管道 #创建管道的类: Pipe([duplex]):在进程之间创建一条管道,并返回元组(conn1,conn2),其中conn1,conn2表示管道两端的连接对象,强调一点:必须在产生Process ...

  3. C++自定义NULLPTR

    惊奇的发现C++中连NULL和nullptr都有区别 NULL和nullptr 根据文章,应当做好NULL和nullptr的兼容工作 翻阅了一下qt的宏定义 #ifdef __GNC__ #defin ...

  4. vue实现购物车和地址选配

    参考文献        vue.js官网 项目演示:数据渲染,格式化数据,点击加,减号自动加减 项目准备 1. 项目css和js文件  https://github.com/4561231/hello ...

  5. IAR STM32 ------ CSTACK HEAP 设置一次可用栈的大小,HardFault_Hander

    CSTACK:限制函数中定义数组的最大值,否则进入HardFault_Hander HEAP:限制动态分配内存(C函数库中的malloc)的大小,不用可以设置为0

  6. bind,apply,call,caller,callee还傻傻分不清楚?

    先介绍每个的语法: 1. bind() 语法:fn.bind(thisObj[, arg1[, arg2[, ...]]]) fn:是想要改变this指向的函数 thisObj:表示fn中this指针 ...

  7. mysql 将一张表的数据更新到另外一张表中

    update 更新表 set 字段 = (select 参考数据 from 参考表 where  参考表.id = 更新表.id); update table_2 m  set m.column = ...

  8. mysql数据库连接useSSL=true

    web应用中连接mysql数据库时控制台会出现这样的提示: Establishing SSL connection without server's identity verification is ...

  9. okhttp post form表单

    OkHttpClient okHttpClient = new OkHttpClient.Builder().build(); FormBody formBody = new FormBody.Bui ...

  10. nGrinder TestRunnerBarrier.groovy / jihedian

    s import net.grinder.script.Barrier import net.grinder.script.GTest import net.grinder.scriptengine. ...