LeetCode(867)
title: LeetCode(867)
tags:
- Python
- Algorithm
题目描述

给定一个矩阵 A, 返回 A 的转置矩阵。
矩阵的转置是指将矩阵的主对角线翻转,交换矩阵的行索引与列索引。
示例 1:
输入:[[1,2,3],[4,5,6],[7,8,9]]
输出:[[1,4,7],[2,5,8],[3,6,9]]
示例 2:
输入:[[1,2,3],[4,5,6]]
输出:[[1,4],[2,5],[3,6]]
提示:
1 <= A.length <= 10001 <= A[0].length <= 1000
解决方案
1.常规复制
class Solution:
def transpose(self, A):
"""
此方法的本质就是ans[c][r] = A[r][c]
:type A: List[List[int]]
:rtype: List[List[int]]
"""
# R——A的长度,C——数组的长度
R,C =len(A),len(A[0])
ans = [[None]*R for _ in range(C)]
# ans [[None, None, None], [None, None, None], [None, None, None]]
for r,row in enumerate(A):
for c,val in enumerate(row):
ans[c][r]= val
return ans
def main():
s =Solution()
b = s.transpose([[1,2,3],[4,5,6],[7,8,9]])
print(b)
#[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
if __name__ == "__main__":
main()
2.zip函数一行搞定
class Solution:
def transpose(self, A):
"""
:type A: List[List[int]]
:rtype: List[List[int]]
"""
return list(zip(*A))
def main():
s =Solution()
b = s.transpose([[1,2,3],[4,5,6],[7,8,9]])
print(b)
#[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
if __name__ == "__main__":
main()
补充:zip函数的用法
描述
zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的对象,这样做的好处是节约了不少的内存。
我们可以使用 list() 转换来输出列表。
如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同,利用 * 号操作符,可以将元组解压为列表。
实例
>>>a = [1,2,3]
>>> b = [4,5,6]
>>> c = [4,5,6,7,8]
>>> zipped = zip(a,b) # 返回一个对象
>>> zipped
<zip object at 0x103abc288>
>>> list(zipped) # list() 转换为列表
[(1, 4), (2, 5), (3, 6)]
>>> list(zip(a,c)) # 元素个数与最短的列表一致
[(1, 4), (2, 5), (3, 6)]
>>> a1, a2 = zip(*zip(a,b)) # 与 zip 相反,zip(*) 可理解为解压,返回二维矩阵式
>>> list(a1)
[1, 2, 3]
>>> list(a2)
[4, 5, 6]
>>>
LeetCode(867)的更多相关文章
- 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_Easy
Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix flipped over it ...
- Leetcode 867. Transpose Matrix
class Solution: def transpose(self, A: List[List[int]]) -> List[List[int]]: return [list(i) for i ...
- LeetCode 867. 转置矩阵
题目链接:https://leetcode-cn.com/problems/transpose-matrix/ 给定一个矩阵 A, 返回 A 的转置矩阵. 矩阵的转置是指将矩阵的主对角线翻转,交换矩阵 ...
- [LeetCode] All questions numbers conclusion 所有题目题号
Note: 后面数字n表明刷的第n + 1遍, 如果题目有**, 表明有待总结 Conclusion questions: [LeetCode] questions conclustion_BFS, ...
- 【LEETCODE】48、867. Transpose Matrix
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 【LeetCode】867. Transpose Matrix 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先构建数组再遍历实现翻转 日期 题目地址:https ...
- 867. Transpose Matrix - LeetCode
Question 867. Transpose Matrix Solution 题目大意:矩阵的转置 思路:定义一个转置后的二维数组,遍历原数组,在赋值时行号列号互换即可 Java实现: public ...
随机推荐
- 用HBuilderX 打包 vue 项目 为 App 的步骤
首先打包你的 vue 项目 生成 dist 文件夹,教程请移步 https://www.cnblogs.com/taohuaya/p/10256670.html 看完上面的教程,请确保 你是 将: ...
- windows 7 下用git
参考:http://my.oschina.net/longxuu/blog/141699
- python 内建函数
# # __geratteibute__class Itcast(object): def __init__(self,subject1): self.subject1 = subject1 self ...
- 关于金蝶k3 wise供应生门户登陆界面屏蔽业务账套多余功能模块设置方法
关于金蝶k3 wise供应生门户登陆界面屏蔽业务账套多余功能模块设置方法 1. 找到以下路径 ...\Kingdee\K3ERP\KDHR\SITEFILE\WEBUI\ 找到“Login.aspx” ...
- Junit4 IDEA测试学习一
1.Junit4 下载 https://github.com/junit-team/junit4/releases 4.12 还需要还导入hamcrest-core-1.3.jar 2.IDEA Te ...
- 虚拟机设置静态IP与配置网络
不废话,直接上图 一:先配置虚拟机的网络 二:配置win10-VM8网络 三:查看以太网的ip信息,直接在管理员命令中输入 ipconfig /all可以到连接信息 四:如果在上面找不到VM8的网络信 ...
- MVC区域area
1.当项目业务比较庞大,可以通过区域来分拆. 2.添加区域时,默认会生成一下文件. 3.Application_Start()必需含有AreaRegistration.RegisterAllAreas ...
- JMeter监控Slave机器是否执行
netstat -anp | grep 192.168.1.161 | grep 19091 | wc -l http://www.linuxidc.com/Linux/2014-09/106497. ...
- 使用java操作kudu
使用maven导入kudu <dependency> <groupId>org.apache.kudu</groupId> <artifactId>ku ...
- git之二: git可视化工具sourcetree
参考: https://www.cnblogs.com/tian-xie/p/6264104.html sourcetree安装使用