zoukankan      html  css  js  c++  java
  • Leetcode#867. Transpose Matrix(转置矩阵)

    题目描述

    给定一个矩阵 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;
        }
    }
    
    
  • 相关阅读:
    VIE模式和IP
    背景色改为豆绿色
    Semantic Logging
    mysql 安装配置相关
    高德API相关
    vmware workstation 虚拟机安装vwmare tools
    sql server2012光盘中有management studio,安装时选择客户端。
    zz微软企业库
    zz flag attribute for enum
    zz 还要用存储过程吗
  • 原文地址:https://www.cnblogs.com/wupeixuan/p/9543358.html
Copyright © 2011-2022 走看看