题目描述

给定一个矩阵 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. Could not install the app on the device, read the error above for details. Make sure you have an Android emulator running or a device connected and have set up your Android development environment:

    Administrator@DESKTOP-EHCTIOR MINGW64 /d/react-native-eyepetizer (master) $ react-native run-android ...

  2. Python基础-元组、列表、字典

    元组tuple 元组被称为只读列表,即数据可以被查询,但不能被修改,所以,字符串的切片操作同样适用于元组.例:(1,2,3)("a","b","c&q ...

  3. python 字典的定义以及方法

    7.字典的转换: dict(x=1,y=2)  ==>  {'y': 2, 'x': 1} dict([(i,element) for i, element in enumerate(['one ...

  4. dubbox知识

    关于dubbox有以下小知识要注意: 1.传参数不能传List参数以及NULL,可以传""和0 2.不能传int类型 3.配置provider的时候,注意不要启动重连机制 < ...

  5. Good Bye 2018 D. New Year and the Permutation Concatenation

    传送门 https://www.cnblogs.com/violet-acmer/p/10201535.html 题意: 求 n 的所有全排列组成的序列中连续的 n 个数加和为 n*(n+1)/2 的 ...

  6. js数组歌

    判断是不是数组,isArray最靠谱. 按照条件来判断,every/some给答案 是否包含此元素,includes最快速. find/findIndex很相似,按条件给第一个值. indexOf/l ...

  7. 【清北学堂2018-刷题冲刺】Contest 7

    Task 1:小奇采药 [问题描述]  小奇是只天资聪颖的喵,他的梦想是成为世界上最伟⼤的医师.  为此,他想拜喵星球最有威望的医师为师.  医师为了判断他的资质,给他出了⼀个难题.  医师把他带到⼀ ...

  8. qml: 支持的基本类型

    qml支持的基本类型有: bool unsigned int, int; float    double   qreal QString QUrl QColor QData,  QTime  QDat ...

  9. OGNL中的#、%和$符号的用法

    转自:https://blog.csdn.net/qq_24963197/article/details/51773224 一.OGNL中的#.%和$符号 1.#符号的三种用法 1)访问非根对象属性, ...

  10. Flink HA 搭建坑

    目前网上能找到的做HA的教程基本都无法真正做到多机高可用,包括官方文档,经过很久的折腾,终于做到了多机高可用,希望其它人不再被坑. 集群模式安装 前提条件: 机器已经安装好Java环境 jobMana ...