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. 1 <= A.length <= 1000
  2. 1 <= A[0].length <= 1000

解法:

class Solution {
public:
    vector<vector<int>> transpose(vector<vector<int>>& A) {
        int m = A.size();
        int n = A[0].size();
        vector<vector<int>> res(n, vector<int>(m, 0));
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                res[j][i] = A[i][j];
            }
        }
        return res;
    }
};

原文地址:https://www.cnblogs.com/zhanzq/p/10663146.html

时间: 2024-10-09 12:38:19

leetcode 867. 转置矩阵(Transpose Matrix)的相关文章

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 <= 10001 &

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_easy】867. Transpose Matrix

problem 867. Transpose Matrix 参考1. Leetcode_easy_867. Transpose Matrix; 完 原文地址:https://www.cnblogs.com/happyamyhope/p/11215040.html

leetcode——Search a 2D Matrix 二维有序数组查找(AC)

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous ro

leetcode第一刷_Spiral Matrix

我觉得这个题好无聊啊,好端端一个数组,干嘛要跟比巴卜一样转一圈输出呢.. 思想很简单,每次从左到右,再从上到下,在从右到左,再从下到上.问题是每次到什么时候该改变方向.我的做法是用一个变量保存当前在第几层,这个层是相对于从外向内有几圈来说的.注意想清楚边界的话这个题一点也不难.有个细节,我的循环退出条件是访问的数跟矩阵总个数之间的关系,如果有一次在判断进入循环是条件是满足的,但是在循环内部不满足了,我的策略是忽略这种情况,虽然这样会在结果集中多加一些重复的数据,但是以我的算法,一定是先访问没有访

[leetcode]Search a 2D Matrix @ Python

原题地址:https://oj.leetcode.com/problems/search-a-2d-matrix/ 题意: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer o

LeetCode: Search a 2D Matrix [074]

[题目] Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previo

[LeetCode] Search a 2D Matrix [25]

题目 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous

leetcode第一刷_Set Matrix Zeroes

这个题乍一看很简单,实际上还挺有技巧的.我最开始的想法是找一个特殊值标记,遇到一个0,把他所对应的行列中非零的元素标记成这个特殊值,0值保持不变,然后再从头遍历一次,碰到特殊值就转化成0. 问题是这个特殊值怎么确定,题目中没有把取值范围给出,我怀着侥幸的心理用了最大和最小的int,都被揪了出来..如果找一个不存在于数组中的值,这个复杂度太高了. 有没有其他更好的方法呢?当然有.这个思想很巧妙,最后的结果是把所有0所在的行列都化成0,换句话说,化成0这个事情只要标记出是哪一行以及哪一列就可以了,能