leetcode-867-Transpose Matrix(矩阵由按行存储变成按列存储)
题目描述:
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 <= A.length <= 10001 <= 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(矩阵由按行存储变成按列存储)的更多相关文章
- Leetcode#867. Transpose Matrix(转置矩阵)
题目描述 给定一个矩阵 A, 返回 A 的转置矩阵. 矩阵的转置是指将矩阵的主对角线翻转,交换矩阵的行索引与列索引. 示例 1: 输入:[[1,2,3],[4,5,6],[7,8,9]] 输出:[[1 ...
- LeetCode 867 Transpose Matrix 解题报告
题目要求 Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix flipped ov ...
- Leetcode 867. Transpose Matrix
class Solution: def transpose(self, A: List[List[int]]) -> List[List[int]]: return [list(i) for i ...
- 867. Transpose Matrix - LeetCode
Question 867. Transpose Matrix Solution 题目大意:矩阵的转置 思路:定义一个转置后的二维数组,遍历原数组,在赋值时行号列号互换即可 Java实现: public ...
- 【LEETCODE】48、867. Transpose Matrix
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 【Leetcode_easy】867. Transpose Matrix
problem 867. Transpose Matrix solution: class Solution { public: vector<vector<int>> tra ...
- 【LeetCode】867. Transpose Matrix 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先构建数组再遍历实现翻转 日期 题目地址:https ...
- 【leetcode】867 - Transpose Matrix
[题干描述] Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix flipped ...
- [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 ...
随机推荐
- Redhat 启动mysql失败及重置密码
cd usr/local/mysql/mysql5.6/bin [root@iZ2ze6yx6b47rdx0vccs5iZ bin]# service mysql-libs-5.1.73-8.el6_ ...
- MongoDB 自动分片 auto sharding
MongoDB部署实验系列文章 MongoDB做为NoSQL数据库,最近几年持续升温,越来越多的企业都开始尝试用MongoDB代替原有Database做一些事情.MongoDB也在集群,分片,复制上也 ...
- JSFF或JSF页面加载时触发JavaScript之方法
现象一 最近在项目中遇到这么一个问题,有些页面元素是在页面加载时通过JavaScript动态渲染而成.当生成这些元素的JavaScript脚本被放置于JSPX文件中时,界面渲染没有问题.但是当我们把生 ...
- mysql查询赋值、修改拼接字符串
sql中修改字符串类型的字段可以这么拼接:update tbName set UserName='abc'+UserName; 但mysql中就不行了,需要这样:update tbName set U ...
- fiddler手机抓包
配置好之后,发现手机不能连接代理服务器,用netstat发现根本就没有手机的ip地址,于是猜想是不是防火墙的问题,关闭防火墙果然能连上,开启防火墙又连不上了,这就说明确实是防火墙的问题,关闭防火墙又很 ...
- Adobe Photoshop CC 2015安装激活教程
Adobe Photoshop CC 2015安装激活教程(附序列号) Adobe Photoshop CC 2015是Adobe针对旗下的创意云Creative Cloud 套装推出了2015年年度 ...
- CentOS 7下面配置静态IP
CentOS 7.0系统是一个很新的版本哦,很多朋友都不知道CentOS 7.0系统是怎么去安装配置的哦,因为centos7.0与以前版本是有很大的改进哦. 说明:截止目前CentOS 7.x最新版本 ...
- Thrift线程和状态机分析
目录 目录 1 1. 工作线程和IO线程 1 2. TNonblockingServer::TConnection::transition() 2 3. RPC函数被调用过程 3 4. 管道和任务队列 ...
- 从Objective-C到Swift 单例模式
在Objective-C中经常会用到单例模式.最常见的就是: [UIApplication sharedApplication].delegate 这里的sharedApplication就是一个返回 ...
- SpringBoot学习:整合shiro自动登录功能(rememberMe记住我功能)
首先在shiro配置类中注入rememberMe管理器 /** * cookie对象; * rememberMeCookie()方法是设置Cookie的生成模版,比如cookie的name,cooki ...