Skip to content

867. Transpose Matrix

https://leetcode.com/problems/transpose-matrix/

js
/**
 * @param {number[][]} A
 * @return {number[][]}
 */
var transpose = function(A) {
  const result = []

  for (let j = 0; j < A[0].length; j++) {
    result[j] = []
    for (let i = 0; i < A.length; i++) {
      result[j][i] = A[i][j]
    }
  }

  return result
}
py
class Solution:

    def transpose(self, A):
        """
        :type A: List[List[int]]
        :rtype: List[List[int]]
        """
        return [[A[i][j] for i in range(len(A))] for j in range(len(A[0]))]