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. 1 <= A.length <= 1000
  2. 1 <= 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)的更多相关文章

  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_Easy

    Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix flipped over it ...

  4. Leetcode 867. Transpose Matrix

    class Solution: def transpose(self, A: List[List[int]]) -> List[List[int]]: return [list(i) for i ...

  5. LeetCode 867. 转置矩阵

    题目链接:https://leetcode-cn.com/problems/transpose-matrix/ 给定一个矩阵 A, 返回 A 的转置矩阵. 矩阵的转置是指将矩阵的主对角线翻转,交换矩阵 ...

  6. [LeetCode] All questions numbers conclusion 所有题目题号

    Note: 后面数字n表明刷的第n + 1遍, 如果题目有**, 表明有待总结 Conclusion questions: [LeetCode] questions conclustion_BFS, ...

  7. 【LEETCODE】48、867. Transpose Matrix

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  8. 【LeetCode】867. Transpose Matrix 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先构建数组再遍历实现翻转 日期 题目地址:https ...

  9. 867. Transpose Matrix - LeetCode

    Question 867. Transpose Matrix Solution 题目大意:矩阵的转置 思路:定义一个转置后的二维数组,遍历原数组,在赋值时行号列号互换即可 Java实现: public ...

随机推荐

  1. 从0开始简单使用git进行项目开发【SourceTree+Coding.net】

    一.什么是git? 含义:Git 是 Linux 发明者 Linus 开发的一款新时代的版本控制系统,相比于原来的svn系统更加简单和实用 作用: 熟悉编程的知道,我们在软件开发中源代码其实是最重要的 ...

  2. SQL Server 常用函数使用方法

    之前就想要把一些 SQL 的常用函数记录下来, 直到今天用到substring()这个函数,C# 里面这个方法起始值是 0,而 SQL 里面起始值是 1.傻傻分不清楚... 这篇博客作为记录 SQL ...

  3. .NET成年了,然后呢?

    作者|Lex Li 编辑|郭蕾 这可能是唯一一篇系统回顾 .NET 发展的文章..NET 的成年礼到了,你会送它什么? 2014 年 11 月 12 日,美国纽约曼哈顿,多云,气温适宜.微软公司执行副 ...

  4. oracle创建用户和密码以及授权登录问题

    创建有户名和密码CREATE USER 用户名 IDENTIFIED BY 密码;分配权限GRANT connect,dba to 用户名; 1:使用oracle的命令行登录oracle的方式(安装好 ...

  5. SpringBoot学习(3)-SpringBoot添加支持CORS跨域访问

    SpringBoot学习(3)-SpringBoot添加支持CORS跨域访问 https://blog.csdn.net/yft_android/article/details/80307672

  6. bzoj4773: 负环

    题解: 网上还有一种spfa+深度限制的算法 https://www.cnblogs.com/BearChild/p/6624302.html 是不加队列优化的spfa,我觉得复杂度上限是bellma ...

  7. 【转】利用 selenium 的 webdrive 驱动 headless chrome

    1.参考 使用 headless chrome进行测试 2.概念 Headless模式解决了什么问题: 自动化工具例如 selenium 利用有头浏览器进行测试,面临效率和稳定性的影响,所以出现了 H ...

  8. WebApi 得到提交过来的 post 数据

    byte[] byts = new byte[System.Web.HttpContext.Current.Request.InputStream.Length]; System.Web.HttpCo ...

  9. 【转】Android 模拟器横屏竖屏切换设置

    http://blog.csdn.net/zanfeng/article/details/18355305# Android 模拟器横屏竖屏切换设置时间:2012-07-04   来源:设计与开发   ...

  10. bash: cannot create temp file for here-document: Read-only file system

    文件系统被强制只读问题,第一眼看到百度了一下,说可能磁盘坏了.卧槽我都吓懵了系统盘坏了,闹着玩呢,然后接着查资料,排查 mount 查看所有挂载,发现根目录的挂载权限是ro只读. /dev/sda2 ...