题目描述:

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. c语言蛋疼的字符串赋值

    我觉得c语言比较蛋疼的一个地方就是给字符串赋值,不是初始化,是赋值. char string[20]={0}; 你不能通过 string="hello";这种方式赋值.但是在字符串 ...

  2. [Selenium]怎样等待元素出现之后再消失,譬如Loading icon

    界面上有些元素是要先等它出现,再等它消失,譬如loading icon 这个是等多个loading icon出现后消失 /** * Wait for loading icon disappear in ...

  3. 界面编程与视图(View)组件

    1.视图组件与容器组件 Android应用绝大部分UI组件都放在Android.widget包及其子包.android.view包及其子包中,其所有UI组件都继承了view类,view组件代表一个空白 ...

  4. 13 Calculating Expected Offspring

    Problem For a random variable XX taking integer values between 1 and nn, the expected value of XX is ...

  5. Kubernetes基本原理与示例

    1. Kubernetes介绍 基本概念 Pod Pod是Kubernetes的基本操作单元,把相关的一个或多个容器构成一个Pod,通常Pod里的容器运行相同的应用.Pod包含的容器运行在同一个Nod ...

  6. C++中const在函数中的用法

    1.const放在函数前面 如果我们的函数的返回值是以 指针形式 返回的,如果在函数前面加上const修饰,则表示指针指向的内容是不能被改变的,并且接收返回值的 指针变量必须是const修饰的,例如: ...

  7. linux每天一小步---find命令详解

    1 命令功能 find命令用于搜索指定目录下的文件,并配合参数做出相应的处理. 2 命令语法      find  搜索路径pathname 选项option [-exec -ok -print  执 ...

  8. java并发编程实战:第六章----任务执行

    任务:通常是一些抽象的且离散的工作单元.大多数并发应用程序都是围绕"任务执行"来构造的,把程序的工作分给多个任务,可以简化程序的组织结构便于维护 一.在线程中执行任务 任务的独立性 ...

  9. ETL工作流缓慢原因查找方法

    What steps do you take to determine the bottleneck of a slow running ETL process? 如果ETL进程运行较慢,需要分哪几步 ...

  10. 浅议Github的注册和使用

    Self-introduction:编者本人叫司明周,现就读于南通大学计算机学院网络工程142班.爱好数学和音乐,喜欢数学中的逻辑性和天马行空的思维 编程能力:可以跳过略过得过且过吗..好吧,面对现实 ...