题目描述:

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. for 续9

    -------siwuxie095                 for 拾遗:         一: for 语句里,do 后面一般会有括号,有括号就是复合语句, 假如需要用到括号里的变量,就需要 ...

  2. des,原理待续

    网络上转载的代码,忘记出处了请原作者见谅! des类 import java.security.*; import javax.crypto.*; /** * DES加解密算法 */ public c ...

  3. 35-Python - 去除list中的空字符

    https://www.cnblogs.com/yspass/p/9434366.html list1 = ['122', '2333', '3444', '', '', None] a = list ...

  4. sql产生随机数字

    第一种:select cast(ceiling(rand() * 10) as int)第二种:select cast(ceiling(rand(checksum(newid()))*10) as i ...

  5. 阿里云Object Storage Service(OSS)

    最近在做一个文件上传.下载的东西,由于上传下载操作频繁.文件存储到独立的服务器, 后来发现阿里云有一项文件存储服务,介绍说很好用,于是就开始使用了. https://help.aliyun.com/d ...

  6. Python爬虫利器六之PyQuery的用法

    前言 你是否觉得 XPath 的用法多少有点晦涩难记呢? 你是否觉得 BeautifulSoup 的语法多少有些悭吝难懂呢? 你是否甚至还在苦苦研究正则表达式却因为少些了一个点而抓狂呢? 你是否已经有 ...

  7. GCT英语口语复试中的常见问题总汇

    英语口语复试中常见的问题: 1. Where do you come from? 2. What kind of landscape surrounds your hometown? 3. What ...

  8. golang C相互调用带参数

    test.h #ifndef __TEST_H__ #define __TEST_H__ void SetFunc(char* str); extern void InternalFunc(char* ...

  9. swoole多进程操作

    多个任务同时执行 将顺序执行的任务,转化为并行执行(任务在逻辑上可以并行执行) 比如,我们要对已知的用户数据进行判断,是否需要发送邮件和短信,如果需要发送则发送. 不使用多进程时,我们首先判断是否发送 ...

  10. solr-DIH:定时增量索引

    参考:官方文档,http://wiki.apache.org/solr/DataImportHandler#Scheduling googlecode 找到:https://code.google.c ...