반응형
https://leetcode.com/problems/transpose-matrix
Transpose Matrix - LeetCode
Can you solve this real interview question? Transpose Matrix - 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. [https://
leetcode.com
class Solution {
public:
vector<vector<int>> transpose(vector<vector<int>>& matrix) {
int n = matrix.size();
int m = matrix.front().size();
vector<vector<int>> res(m, vector<int>(n));
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
res[j][i] = matrix[i][j];
}
}
return res;
}
};
반응형
'Algorithm' 카테고리의 다른 글
LeetCode 1464. Maximum Product of Two Elements in an Array (0) | 2023.12.12 |
---|---|
LeetCode 1287. Element Appearing More Than 25% In Sorted Array (0) | 2023.12.12 |
LeetCode 94. Binary Tree Inorder Traversal (0) | 2023.12.09 |
LeetCode 606. Construct String from Binary Tree (0) | 2023.12.09 |
LeetCode 1903. Largest Odd Number in String (1) | 2023.12.07 |