zoukankan      html  css  js  c++  java
  • 矩阵转置

    一、题目描述

     给定一个矩阵 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]]

     方法一:

    class Solution:
        def transpose(self, A):
            """
            :type A: List[List[int]]
            :rtype: List[List[int]]
            """
            result = []
            
            for i in range(len(A[0])):
                temp = []
                for j in range(len(A)):
                    temp.append(A[j][i])
                result.append(temp)
                    
            return result
    

     方法二:

    class Solution:
        def transpose(self, A):
            """
            :type A: List[List[int]]
            :rtype: List[List[int]]
            """
            A = list( map( list, zip(*A) ) )
            return A
            
    

      

  • 相关阅读:
    things to analysis
    retrieve jenkins console output
    temp
    temp
    mysql on Mac OS
    Scala的几个小tips
    docker查看容器使用率
    JVM性能监控(jconsole和jvisualvm)
    线程死锁问题
    线程阻塞状态
  • 原文地址:https://www.cnblogs.com/always-fight/p/10333179.html
Copyright © 2011-2022 走看看