Re: [閒聊] 每日LeetCode
867. Transpose Matrix
https://leetcode.com/problems/transpose-matrix/description/
給定一個二維矩陣,
傳回欄列對調的結果(轉置矩陣)
--
還挺簡單,
Python甚至可以一行解
最近怎麼都簡單題= =
--
Python Code:
class Solution:
def transpose(self, matrix: List[List[int]]) -> List[List[int]]:
m, n = len(matrix), len(matrix[0])
return [[matrix[j][i] for j in range(m)] for i in range(n)]
C++ Code:
class Solution {
public:
vector<vector<int>> transpose(vector<vector<int>>& matrix) {
int m = matrix.size();
int n = matrix[0].size();
vector<vector<int>> ans(n, vector<int>(m, 0));
for (int i = 0;i < n;i++){
for (int j = 0;j < m;j++){
ans[i][j] = matrix[j][i];
}
}
return ans;
}
};
--
咖啡是一種豆漿,
茶是一種蔬菜湯。
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.161.65.130 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1702199613.A.3D0.html
推
12/10 17:13,
2年前
, 1F
12/10 17:13, 1F
→
12/10 17:14,
2年前
, 2F
12/10 17:14, 2F
推
12/10 17:16,
2年前
, 3F
12/10 17:16, 3F
→
12/10 17:56,
2年前
, 4F
12/10 17:56, 4F
討論串 (同標題文章)
完整討論串 (本文為第 571 之 719 篇):