Vison's Blog
所有文章
文章总览
编辑器
Publications
About Me
Search
目录
#toc-container
下载Markdown文件
【2.25】LeetCode每日一题·转置矩阵
2021年02月25日 9时09分
标签:
LeetCode
数组
## 题目描述 给你一个二维整数数组 matrix,返回 matrix 的转置矩阵 。 矩阵的 转置 是指将矩阵的主对角线翻转,交换矩阵的行索引与列索引。 [](https://assets.leetcode.com/uploads/2021/02/10/hint_transpose.png "转置矩阵") #### 示例1 输入:matrix = [[1,2,3],[4,5,6],[7,8,9]] 输出:[[1,4,7],[2,5,8],[3,6,9]] #### 示例2 输入:matrix = [[1,2,3],[4,5,6]] 输出:[[1,4],[2,5],[3,6]] #### 提示 m == matrix.length n == matrix[i].length 1 <= m, n <= 1000 1 <= m * n <= 10^5 -10^9 <= matrix[i][j] <= 10^9 #### 来源 > 来源:力扣(LeetCode) > 链接:[题目链接](https://leetcode-cn.com/problems/transpose-matrix "题目链接") ## 解题思路 对于$$N\times M$$的矩阵,其转置矩阵的大小为$$M \times N$$,且``reverse[i][j] == matrix[j][i]``。模拟即可。 ##代码 #!C++ class Solution { public: vector
> transpose(vector
>& matrix) { const int N = matrix.size(), M = matrix[0].size(); vector
> res(M, vector
(N)); for(int i = 0; i < M; i++){ for(int j = 0; j < N; j++){ res[i][j] = matrix[j][i]; } } return res; } }; ## 用时和内存 > 执行用时:8 ms,在所有 C++提交中击败了98.57%的用户 > 内存消耗:10.4 MB,在所有 C++提交中击败了8.28%的用户
所有评论
暂无评论
新增评论
评论
邮箱
邮箱仅作验证使用
图形验证码
邮箱验证码
发送验证码
发表评论
所有评论
暂无评论