LeetCode 867. 转置矩阵

题目链接:https://leetcode-cn.com/problems/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 <= A.length <= 1000
1 <= A[0].length <= 1000

 1 class Solution {
 2     public int[][] transpose(int[][] A) {
 3         int[][] newA=new int[A[0].length][A.length];
 4         for(int i=0;i<A.length;i++){
 5             for(int j=0;j<A[0].length;j++){
 6                 newA[j][i]=A[i][j];
 7             }
 8         }
 9         return newA;
10     }
11 }

原文地址:https://www.cnblogs.com/shixinzei/p/11595002.html

时间: 2024-08-07 00:11:00

LeetCode 867. 转置矩阵的相关文章

leetcode 867. 转置矩阵(Transpose Matrix)

目录 题目描述: 示例 1: 示例 2: 解法: 题目描述: 给定一个矩阵 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 <= A.length <= 1000 1 <= A[0].length <

867. 转置矩阵 ?

原文 给定一个矩阵 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 <= A.length <= 1000 1 <= A[0].length <= 1000 class Solution { publ

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. 题目分析及思路 题目要求得到矩阵的转置矩阵.可先得到一个行数与原矩阵列数相等.列数与原矩阵行数相等的矩阵,再对原矩阵进行遍历. python代码? c

【Golang语言版本】LeetCode 867. Transpose Matrix 矩阵转置

矩阵转置,A[i][j] 变成A[j][i] 比较简单,直接上代码了. func transpose(A [][]int) [][]int { B := make([][]int, len(A[0])) for i := 0; i < len(A[0]); i++ { B[i] = make([]int, len(A)) for j := 0; j < len(A); j++ { B[i][j] = A[j][i] } } return B } 原文地址:https://blog.51cto.

leet

# 题名1 两数之和    2 两数相加    3 无重复字符的最长子串    4 寻找两个有序数组的中位数    5 最长回文子串    6 Z 字形变换    7 整数反转    8 字符串转换整数 (atoi)    9 回文数    10 正则表达式匹配    11 盛最多水的容器    12 整数转罗马数字    13 罗马数字转整数    14 最长公共前缀    15 三数之和    16 最接近的三数之和    17 电话号码的字母组合    18 四数之和    19 删除链表

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; i < A.length; i++) { for (int j = 0; j < A[0].length; j++

LeetCode——48. 旋转图像

给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使用另一个矩阵来旋转图像. 示例 1: 给定 matrix = [ [1,2,3], [4,5,6], [7,8,9] ], 原地旋转输入矩阵,使其变为: [ [7,4,1], [8,5,2], [9,6,3] ] 示例 2: 给定 matrix = [ [ 5, 1, 9,11], [ 2, 4, 8,10], [13, 3, 6, 7], [

[LeetCode] 349 Intersection of Two Arrays &amp; 350 Intersection of Two Arrays II

这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/intersection-of-two-arrays/description/ 350 Intersection of Two Arrays II:https://leetcode.com/problems/intersection-of-two-arrays-ii/description/ 题目&解法

LeetCode 442. Find All Duplicates in an Array (在数组中找到所有的重复项)

Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements that appear twice in this array. Could you do it without extra space and in O(n) runtime? Example: Input: [4,3,2,7,