题目描述:

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

要完成的函数:

vector<vector<int>> transpose(vector<vector<int>>& A)

说明:

1、给定一个二维vector,命名为A,要求把A转置,输出转置之后的二维vector。

不过这里的二维vector不一定是方阵(也就是行数和列数不一定相等)。

比如[[1,2,3],[4,5,6]],转置之后结果是[[1,4],[2,5],[3,6]],其实也就是按列读取的结果。

2、明白题意,这道题一点也不难。

代码如下:(附详解)

    vector<vector<int>> transpose(vector<vector<int>>& A)
{
int hang=A.size(),lie=A[0].size();//得到行数和列数
vector<vector<int>>res;
vector<int>res1;
for(int j=0;j<lie;j++)//外层循环是列的循环
{
for(int i=0;i<hang;i++)//内层循环是行的循环
{
res1.push_back(A[i][j]);//不断地把每一行同一列的值插入到res1中去
}
res.push_back(res1);//res1的结果插入到res中
res1.clear();//清空res1
}
return res;
}

上述代码十分简洁,就是vector的不断插入比较费时间。我们也可以改成先定义好二维vector的长度,接着更新数值就好,这样就不用频繁地申请内存空间了。

但对于这道题,花费的时间没有相差太多。

上述代码实测16ms,beats 98.72% of cpp submissions。

leetcode-867-Transpose Matrix(矩阵由按行存储变成按列存储)的更多相关文章

  1. Leetcode#867. Transpose Matrix(转置矩阵)

    题目描述 给定一个矩阵 A, 返回 A 的转置矩阵. 矩阵的转置是指将矩阵的主对角线翻转,交换矩阵的行索引与列索引. 示例 1: 输入:[[1,2,3],[4,5,6],[7,8,9]] 输出:[[1 ...

  2. LeetCode 867 Transpose Matrix 解题报告

    题目要求 Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix flipped ov ...

  3. Leetcode 867. Transpose Matrix

    class Solution: def transpose(self, A: List[List[int]]) -> List[List[int]]: return [list(i) for i ...

  4. 867. Transpose Matrix - LeetCode

    Question 867. Transpose Matrix Solution 题目大意:矩阵的转置 思路:定义一个转置后的二维数组,遍历原数组,在赋值时行号列号互换即可 Java实现: public ...

  5. 【LEETCODE】48、867. Transpose Matrix

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  6. 【Leetcode_easy】867. Transpose Matrix

    problem 867. Transpose Matrix solution: class Solution { public: vector<vector<int>> tra ...

  7. 【LeetCode】867. Transpose Matrix 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先构建数组再遍历实现翻转 日期 题目地址:https ...

  8. 【leetcode】867 - Transpose Matrix

    [题干描述] Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix flipped ...

  9. [LeetCode&Python] Problem 867. Transpose Matrix

    Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix flipped over it ...

随机推荐

  1. Multiply Strings大整数乘法

    [抄题]: 以字符串的形式给定两个非负整数 num1 和 num2,返回 num1 和 num2 的乘积. [暴力解法]: 时间分析: 空间分析: [思维问题]: 还要找到结果中第一位不等于0的数再添 ...

  2. Linux运维实战之DNS(bind)服务器的安装与配置

    转自http://sweetpotato.blog.51cto.com/533893/1598225 上次博文我们讨论了DNS的基础,本次博文我们重点来看看如何配置一台DNS服务器. [本次博文的主要 ...

  3. ibatis 常用标签

    prepend:自动在前面加上:自动新手:自动预:自动前置 property:属性 compareValue:指定的常数,值 //判断不相等: <isNotEqual prepend=" ...

  4. jave 逻辑运算 vs 位运算 + Python 逻辑运算 vs 位运算

    JAVA中&&和&.||和|(短路与和逻辑与.短路或和逻辑或)的区别 博客分类: 面试题目 Java.netBlog  转自 :http://blog.csdn.net/web ...

  5. Ckeditor 中粘贴图片

    我们在ckeditor 中有上传图片,但是实际使用中这种手动上传图片方式并不是很方便,而是复制或者截图粘贴图片. 这里我们实现主要是获取对应的粘贴事件. CKEDITOR.instances[&quo ...

  6. Monokai风格的EditPlus配色方案

    EditPlus的配置文件editplus_u.ini,该文件默认在:系统盘:\Users\用户名\AppData\Roaming\EditPlus目录中.将其中的内容替换为如下即可: [Option ...

  7. myeclispe2014启动后报错 Subclipse talks to Subversion via a Java API that requires access to native libraries.

    解决方案: Window -> Preferences -> Team -> SVN, 将SVN接口的Client修改为如图所示

  8. UVa 1395 Slim Span (最小生成树)

    题意:给定n个结点的图,求最大边的权值减去最小边的权值最小的生成树. 析:这个和最小生成树差不多,从小到大枚举左端点,对于每一个左端点,再枚举右端点,不断更新最小值.挺简单的一个题. #include ...

  9. word 写博客,直接上传

    目前大部分的博客作者在用Word写博客这件事情上都会遇到以下3个痛点: 1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.使用Word写 ...

  10. 一个GIS系统需具备的功能

    数据的存储(数据结构) 图形显示 功能:拓扑分析 开源的GIS系统:QGIS,SharpMap 各个功能组件