알고리즘/해결

LeetCode867. Transpose Matrix

언클린 2020. 4. 5. 13:48
728x90

1. 문제(원본)

Given a matrix A, return the transpose of A.

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

 

 

Example 1:

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

Example 2:

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

 

Note:

  1. 1 <= A.length <= 1000
  2. 1 <= A[0].length <= 1000

2. 문제

입력받은 이차원 배열을 회전시켜라

3. 나의 답

class Solution {

    func transpose(_ A: [[Int]]) -> [[Int]] {

        var result:[[Int]] = []

        for index in 0..<A.first!.count {

            var makeArray:[Int] = []

            for target in A {

                makeArray.append(target[index])

            }

            result.append(makeArray)

        }

        return result

    }

}

4. 다른 유저의 답

대부분의 답이 비슷한 유형으로 풀어져 있었다.

5. 마무리

이차원 배열에 관한 문제도 하나둘씩 풀어나가면서 점점 익숙해지는 것 같은 기분이 들어 별 어려움 없이 풀었던 것 같다.

728x90

'알고리즘 > 해결' 카테고리의 다른 글

LeetCode868. Binary Gap  (0) 2020.04.12
LeetCode283. Move Zeroes  (0) 2020.04.12
LeetCode1046. Last Stone Weight  (0) 2020.04.05
LeetCode344. Reverse String  (0) 2020.03.31
LeetCode557. Reverse Words in a String III  (0) 2020.03.31