题目描述

给定一个矩阵 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(二十)model中的 class Meta

    https://www.cnblogs.com/tongchengbin/p/7670927.html class Main(models.Model): img = models.CharField ...

  2. Linux安装 火速入门

    一.基本简介 Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和UNIX的多用户.多任务.支持多线程和多CPU的操作系统. Linux能运行主要的UNIX工具软件.应用程序 ...

  3. 第一篇 - bsp抓取python中文开发者社区中的所有高级教程

    工具:python3.6  pycharm 库:bs4 + urllib 第一步:读取html源码 from bs4 import BeautifulSoup import urllib.reques ...

  4. 第十四节,卷积神经网络之经典网络Inception(四)

    一 1x1卷积 在架构内容设计方面,其中一个比较有帮助的想法是使用 1×1 卷积.也许你会好奇,1×1 的卷积能做什么呢?不就是乘以数字么?听上去挺好笑的,结果并非如此,我们来具体看看. 过滤器为 1 ...

  5. tensorflow中tf.ConfigProto()用法解释

    在看C3D代码的时候,看见有一段代码是 config = tf.ConfigProto()#主要是配置tf.Session的运行方式,GPU还是CPU,在这里选择的是GPU的运行方式 config.g ...

  6. c#反射(1)

    反射可以读取程序集中代码的内容,程序集一般指(dll或exe文件). 反射中Type类,这个类太强大了,可以获取到另一个类的名称,命名空间,程序集,以及这个类中的字段,属性,方法.可以方便我们查看某个 ...

  7. POJ 2823 Sliding Window (单调队列)

    单调队列 加了读入挂比不加更慢.... 而且这份代码要交c++ 有大神G++跑了700ms..... orzorzorz #include<iostream> #include<cs ...

  8. as报错

    android studio报错报错一:Could not HEAD 'https://dl.google.com/dl/android/maven2/com/android/tools/build/ ...

  9. Maven Tomcat7+ 实现自动化部署

    首先在Tomcat里配置deploy的用户(tomcat根目录/conf/tomcat-users.xml): <role rolename="tomcat"/> &l ...

  10. Object的数据属性和访问器属性

    一.数据属性 1.数据属性:它包含的是一个数据值的位置,在这可以对数据值进行读写. 2.数据属性包含四个特性,分别是: configurable:表示能否通过delete删除属性从而重新定义属性,能否 ...