Leetcode 867. Transpose Matrix, Java解法

Java_转置矩阵(Transpose) 题目 class Solution { public int[][] transpose(int[][] A) { int[][] B=new int[A[0].length][A.length]; for(int i=0;i<B.length;i++) { for(int j=0;j<B[i].length;j++) { ... 阅读详情

Given a matrix A, return the transpose of A.

The transpose of a matrix is the matrix flipped over it's main diagonal, switching the row and column indices of the matrix.

Example 1:

Input: [[1,2,3],[4,5,6],[7,8,9]]
Output: [[1,4,7],[2,5,8],[3,6,9]]

Example 2:

Input: [[1,2,3],[4,5,6]]
Output: [[1,4],[2,5],[3,6]]

Note:

  1. 1 <= A.length <= 1000
  2. 1 <= A[0].length <= 1000

Approach #1 Brute Force, Copy Directly 我的解法

class Solution {
    public int[][] transpose(int[][] A) {
    int row = A.length;
    int col = A[0].length;
    int[][] ans = new int[col][row];
    for(int i=0;i<col;++i){
        for(int j=0;j<row;++j){
            ans[i][j] = A[j][i];
        }
    }
    return ans;
    }
}

Complexity Analysis

  • Time Complexity: O(R * C), where R and C are the number of rows and columns in the given matrix A.

  • Space Complexity: O(R * C), the space used by the answer. 

Tips:

需要注意这个矩阵不一定是方的,审题要注意。

JavaProgramToTransposeMatrix.pdf 英文原版 Java Program To Transpose Matrix 立即下载

相关推荐

使用Matlab进行IEEE 33节点潮流计算的NR法实现详解及应用实例

IEEE 33节点系统是一个标准的配电系统模型,广泛用于学术研究和工程应用。该系统由33个节点和37条线路组成,结构复杂,具有代表性。其潮流计算结果可用于验证各种潮流计算方法的准确性和效率。NR法是一种基于迭代求解的潮流计算方法,通过不断迭代求解非线性方程组,直到达到预定的收敛条件。NR法具有收敛速度快、计算精度高的优点,适用于大规模复杂电力系统的潮流计算。

m0_57781768的博客 2070

线性代数:转置矩阵(matrix transpose)和逆矩阵(matrix inverse)

       这一篇是为了后面着色效果的数学基础做积累,之前我们使用矩阵的大部分情况都是直接的仿射空间变换,就是仿射空间A变换到仿射空间B,使用矩阵也都是如下:        矩阵T*齐次坐标V = 齐次坐标V'        其计算细节也就是矩阵行与向量列的点积,其计算意义也就是获得新仿射空间中的坐标分量,也聊了很多了。        这次我们就来学两个矩阵的操作,一个是矩阵的转置操作(得...

羊羊的博客 26万+

USB摄像头开发实战:从参数配置到图像采集的完整流程(Python+OpenCV)

本文详细介绍了USB摄像头开发的完整流程,从参数配置到图像采集,使用Python和OpenCV实现。内容涵盖硬件原理、环境配置、参数调优及高级优化技巧,帮助开发者解决常见问题如分辨率设置、帧率控制和灯光频闪等,提升开发效率。

star的博客 235

LeetCode-Java-867. Transpose Matrix

题目 Given a matrix A, return the transpose of A. 给定一个矩阵A,返回A的转置 The transpose of a matrix is the matrix flipped over it's main diagonal, switching the row and column indices of the matrix. 矩阵的转置是在其对...

咸鱼塘 544

MOS管关断时尖峰电压的5种实用抑制方案(附实测数据对比)

本文针对MOS管关断时产生的尖峰电压问题,提供了五种经过实验室实测验证的实用抑制方案。文章从尖峰电压的成因分析入手,详细阐述了优化PCB布局、设计RC缓冲电路、选用TVS二极管钳位、调节驱动电路以及使用磁珠吸收高频振荡等方法的原理、实施步骤与实测数据对比,为硬件工程师解决开关电源和电机驱动中的过压应力与EMI问题提供了系统性的实战指南。

1239

【TM1650】STM8S TM1650芯片控制程序

以下为.h文件: #ifndef __TM1650_I2C_H #define __TM1650_I2C_H #include "stm8s.h" #include "stm8s_gpio.h" #include "tim1.h" #include "usart2.h" #include &lt;iostm8s103f3.h&gt; #include &lt;intrinsics....

cxw312864660的博客 1万+

Java/868.(装置矩阵) 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 &lt;= A.length &lt;= 1000...

qq_38959715的博客 701

Python 使用Pygame库实现足球游戏:创建一个球员和一个足球,并使玩家能够控制球员的移动。游戏将包括碰撞检测、简单的物理模拟,以及基本的用户输入处理

本文将介绍如何使用Python和Pygame库创建一个简单的足球游戏。我们将搭建一个基本的游戏环境,创建一个球员和一个足球,并使玩家能够控制球员的移动。游戏将包括碰撞检测、简单的物理模拟,以及基本的用户输入处理。这个简单的足球游戏展示了Pygame的一些基本功能。虽然这是一个简单的游戏,但它包含了创建游戏循环、用户输入处理、物理模拟、碰撞检测等重要概念。

程序熊的博客 743

Leetcode学习笔记:#867. Transpose Matrix

Leetcode学习笔记:#867. Transpose Matrix Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix flipped over it’s main diagonal, switching the row and column indices of the ma...

ccystewart的博客 146

全网首发--Unet3+改进16:在不同位置添加RepLKBlock超大卷积核

本文提出了一种基于大核卷积的神经网络架构RepLKNet,旨在缩小CNN与ViTs之间的性能差距。通过重新审视CNN中的大核设计,研究证明使用少量大卷积核替代多个小核能获得更强大的性能。提出的RepLKNet采用31×31大卷积核,在ImageNet和下游任务上取得了与SwinTransformer相当或更好的结果,同时具有更低延迟。文章详细介绍了实现步骤:(1)创建RepLKBlock模块文件;(2)在模型层中添加注意力机制;(3)在UNet模型中集成RepLKBlock;(4)完成训练流程。该方法展现了

AIcurator的博客 616

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

【LeetCode】867. Transpose Matrix 转置矩阵(Easy)(JAVA)

【LeetCode】867. Transpose Matrix 转置矩阵(Easy)(JAVA) 题目地址: https://leetcode.com/problems/transpose-matrix/ 题目描述: Given a 2D integer array matrix, return the transpose of matrix. The transpose of a matrix is the matrix flipped over its main diagonal, switching

qq_16927853的博客 429

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

LeetCode.867-转置矩阵(Transpose Matrix)

这是悦乐书的第332次更新,第356篇原创 01看题和准备 今天介绍的是LeetCode算法题中Easy级别的第202题(顺位题号是867)。给定矩阵A,返回A的转置。 矩阵的转置是在其主对角线上翻转的矩阵,切换矩阵的行和列索引。例如: 输入:[[1,2,3],[4,5,6],[7,8,9]] 输出:[[1,4,7],[2,5,8],[3,6,9]] 输入:[[1,2,3],[4,5,6]] ...

小川的博客 312

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

【Leetcode】867. Transpose Matrix

题目地址: https://leetcode.com/problems/transpose-matrix/ 给定一个矩阵,求其转置。 注意,给出的矩阵未必是方阵,所以要另外开一个二维数组返回。代码如下: public class Solution { public int[][] transpose(int[][] A) { if (A == null || A.length == 0 || A[0].length == 0) { return A;

数学、算法爱好者的博客 164

[leetcode] 867. Transpose Matrix

题目: Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix flipped over it's main diagonal, switching the row and column indices of the matrix.   Example 1: Input: ...

jing16337305的博客 384

【leetcode】867. Transpose Matrix

class Solution { public int[][] transpose(int[][] A) { int row = A.length; int column = A[0].length; int[][] B = new int[column][row]; for(int i=0; i<r...

Art1st_D的博客 241

LeetCode - 867 - 转置矩阵(transpose-matrix)

一 目录 不折腾的前端,和咸鱼有什么区别目录一 目录二 前言三 解题及测试四 LeetCode Submit五 解题思路六 进一步思考二 前言 难度:简单涉及知识:数组题目地址:http...

weixin_41806099的博客 258

LeetCode867. Transpose Matrix

Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix flipped over it's main diagonal, switching the row and column indices of the matrix. Example 1: Input: [[1,2,3]...

帽帽的博客 501

【Leetcode】 867. Transpose Matrix 转置矩阵

Problem Leetcode Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix flipped over it’s main diagonal, switching the row and column indices of the matrix. Example Example1: Input: [[1,2,3],[4,5,6],[7,8,9]] Output: [[1,4,7],

Moodles 168

LeetCode867. Transpose Matrix(Java实现)

问题描述 Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix flipped over it’s main diagonal, switching the row and column indices of the matrix. 解决方案 代码如下: public int...

超悦人生 293

leetcode 867. Transpose Matrix(转置矩阵)

把矩阵转置。

蓝羽飞鸟的博客 337

867. Transpose Matrix - LeetCode

Question 867. Transpose Matrix Solution 题目大意:矩阵的转置 思路:定义一个转置后的二维数组,遍历原数组,在赋值时行号列号互换即可 Java实现: public int[][] transpose(int[][] A) { int[][] B = new int[A[0].length][A.length]; for (int i = 0;...

weixin_30273175的博客 87

[LeetCode] 867. Transpose Matrix

Problem Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix flipped over it's main diagonal, switching the row and column indices of the ...

weixin_34378922的博客 80
上一篇: Leetcode 1002. Find Common Characters, Java 解法
下一篇: Leetcode [724, 747,66] Array专题 (1)
YuanTheCoder
博客等级 码龄9年 2粉丝 34原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值