题目描述:

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. 优化 resolv.conf

    DNS lookup 在把域名解析成 IP 过程中耽误了不少时间,尤其是访问比较复杂的网站的时候,比如某个页面包含多个 url,一次请求需要做多次 DNS 解析,并且从用户浏览器到 DNS serve ...

  2. C# Redis Server分布式缓存编程(二)(转)

    出处;http://www.cnblogs.com/davidgu/p/3263485.html 在Redis编程中, 实体和集合类型则更加有趣和实用 namespace Zeus.Cache.Red ...

  3. Leader/Follower多线程网络模型介绍

    之前分享过<轻量级 web server Tornado代码分析>,介绍了目前我们采用nginx + tornado的方式搭建升级.配管.数据中心等各类服务组建客户端迭代体系.最近注意到, ...

  4. Debug_with_docker_in_pycharm

    Debug with docker in pycharm Why As I really really appreciate it that we can have a isolated develo ...

  5. 试题 G: 外卖店优先级 第十届蓝桥杯

    试题 G: 外卖店优先级时间限制: 1.0s 内存限制: 512.0MB 本题总分: 20 分[问题描述]“饱了么”外卖系统中维护着 N 家外卖店,编号 1 ∼ N.每家外卖店都有一个优先级,初始时 ...

  6. Python学习-11.Python中的类定义

    Python是一门面向对象语言,那么作为面向对象的特征——类也是有的.值得注意的是Python中一切皆对象,并不像C#中为了性能考虑,int这些在Python中也是对象.(C#中int是结构体) 如何 ...

  7. [转]解决Mysql InnoDB: Failing assertion: ret || !assert_on_error问题

    国庆回来后,发现mysql停止服务了,没办法继续启动了.查看日志,看到: 131008 09:56:03 mysqld_safe Starting mysqld daemon with databas ...

  8. Math类的三个方法比较: floor() ceil() round()

    public class Test { public static void main(String[] args) { double d1 = 3.4, d2 = 3.6; //正数 double ...

  9. 原创:MVC 5 实例教程(MvcMovieStore 新概念版:mvc5.0,EF6.01) - 1、初露锋芒

    说明:MvcMovieStore项目已经发布上线,想了解最新版本功能请登录 MVC 影视(MvcMovie.cn) 进行查阅.如需转载,请注明出处:http://www.cnblogs.com/Dod ...

  10. sweetalert 快速显示两个提示, 第二个显示不出的问题

    今天在使用 sweetalert 做提示框的时候, 有个操作快速做了两次提示, 发现第二次显示不出: sweetAlert({}, function() { $.get('', function() ...