LeetCode 솔루션 분류

[6/2] 867. Transpose Matrix

컨텐츠 정보

본문

867. Transpose Matrix
Easy
2013396Add to ListShare

Given a 2D integer array matrix, return the transpose of matrix.

The transpose of a matrix is the matrix flipped over its main diagonal, switching the matrix's row and column indices.

 

Example 1:

Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [[1,4,7],[2,5,8],[3,6,9]]

Example 2:

Input: matrix = [[1,2,3],[4,5,6]]
Output: [[1,4],[2,5],[3,6]]

 

Constraints:

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 1000
  • 1 <= m * n <= 105
  • -109 <= matrix[i][j] <= 109

관련자료

댓글 5

나무토끼님의 댓글

  • 익명
  • 작성일
class Solution:
    def transpose(self, matrix: List[List[int]]) -> List[List[int]]:
        r = len(matrix)
        c = len(matrix[0])
        ans = [[0] * r for _ in range(c)]
        for i in range(r):
            for j in range(c):
                if i != j:
                    ans[j][i] = matrix[i][j]
                else:
                    ans[i][j] = matrix[i][j]
        return ans

재민재민님의 댓글

  • 익명
  • 작성일
class Solution {
public:
    vector<vector<int>> transpose(vector<vector<int>>& matrix) {
        int row = matrix.size();
        int col = matrix[0].size();
        
        
            vector<vector<int>> ret(col, vector<int>(row, 0)) ;
        
            for(int i = 0; i < row; i++){
                for(int j = 0; j < col; j++){                
                    ret[j][i] = matrix[i][j];
                }            
            }
            return ret;    
        /*
        else{
            int tmp = 0;
            
            for(int i = 0; i < row; i++){
                for(int j = 0; j < row; j++){
                    if(j == row-1-i)
                        break;
                    
                    // [0,row-1] [1, row-2], [2, row-3]
                    // [0, 2] 
                    tmp = matrix[i][j];
                    matrix[i][j] = matrix[j][i];
                    matrix[j][i] = tmp;
                }
            }            
            
            return matrix;
        }
        */
        
    }
};

학부유학생님의 댓글

  • 익명
  • 작성일
Python3
Runtime: 131 ms, faster than 21.67% of Python3 online submissions for Transpose Matrix.
Memory Usage: 14.7 MB, less than 55.25% of Python3 online submissions for Transpose Matrix.

class Solution:
    def transpose(self, matrix: List[List[int]]) -> List[List[int]]:
        ROW, COL = len(matrix), len(matrix[0])
        
        res = [ [matrix[0][i]] for i in range(COL) ]
        
        for item in matrix[1:]:
            for i in range(COL):
                res[i].append(item[i])
        
        return res
            

Jack님의 댓글

  • 익명
  • 작성일
Python3

Runtime: 144 ms, faster than 12.24% of Python3 online submissions for Transpose Matrix.
Memory Usage: 14.9 MB, less than 16.79% of Python3 online submissions for Transpose Matrix.

class Solution:
    def transpose(self, matrix: List[List[int]]) -> List[List[int]]:
        row = len(matrix)
        col = len(matrix[0])

        if row != col:
            result = [[None] * row for _ in range(col)]
            for r, rval in enumerate(matrix):
                for c, cval in enumerate(rval):
                    result[c][r]=cval
            return result
        else:
            for c in range(col):
                for r in range(row):
                    if c != r and r > c:
                        matrix[c][r], matrix[r][c] = matrix[r][c], matrix[c][r]
            return matrix

Coffee님의 댓글

  • 익명
  • 작성일
class Solution {
    public int[][] transpose(int[][] matrix) {
        int row = matrix.length;
        int col = matrix[0].length;
        
        int[][] result = new int[col][row];
        
        for(int i=0; i<row; i++){
            for(int j=0; j<col; j++){
                result[j][i] = matrix[i][j];
            }
        }
        
        return result;
    }
    
    
}
LeetCode 솔루션 21 / 1 페이지
번호
제목
이름

최근글


인기글


새댓글


Stats


  • 현재 접속자 730 명
  • 오늘 방문자 6,083 명
  • 어제 방문자 7,431 명
  • 최대 방문자 14,831 명
  • 전체 회원수 1,542 명
알림 0