๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Algorithm/Leetcode

[Leetcode] Transpose Matrix (#867)

 

 

๋ฌธ์ œ

 

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]]

 

 

ํ•ด์„ค ์ฝ”๋“œ 
class Solution(object):
    def transpose(self, matrix):
        arr = [[0 for i in range(len(matrix))] for t in range(len(matrix[0]))]
        
        for i in range(len(matrix)):
            for t in range(len(matrix[0])):
                arr[t][i] = matrix[i][t]
        
        return arr
๋ถ„์„ > ์ด ๋ฌธ์ œ์—์„œ ๊ฐ„๊ณผํ–ˆ๋˜ ๋ถ€๋ถ„์€ ์ด์ค‘ ๋ฐฐ์—ด์„ ์„ ์–ธํ•  ๋•Œ ๋‹จ์ˆœํ•˜๊ฒŒ ๊ณฑํ•˜๊ธฐ๋ฅผ ์ด์šฉํ•ด์„œ ๋ฐฐ์—ด์„ ์„ ์–ธํ–ˆ๋‹ค๋Š” ์ ์ด๋‹ค. ๊ทธ๋ž˜์„œ ์•ˆ์— ์žˆ๋Š” ๋ฐฐ์—ด์ด ๋˜‘๊ฐ™์ด ์›€์ง์ด๊ฒŒ ๋˜์–ด ์›ํ•˜๋Š” ๋‹ต์ด ๋‚˜์˜ฌ ์ˆ˜ ์—†์—ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ ์ด์ค‘ ๋ฐฐ์—ด์„ ์„ ์–ธํ•  ๋•Œ๋Š” for๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ์•ผ ํ•œ๋‹ค. 

 

 

 

 

Transpose Matrix - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

'Algorithm > Leetcode' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Leetcode] Print Words Vertically (#1324)  (0) 2022.04.20
[Leetcode] Reshape the Matrix (#566)  (0) 2022.04.20
[Leetcode] Complex Number Multiplication (#537)  (0) 2022.04.20
[Leetcode] Game of Life (#289)  (0) 2022.04.19
[Leetcode] Last Stone Weight (#1046)  (0) 2022.04.08