leetcode 867. Transpose Matrix
class Solution { public: vector<vector<int>> transpose(vector<vector<int>>& A) { int m=A.size(); int n=A[0].size(); if(m==n){ for(int i=0;i<m;i++){ for(int j=0;j<i;++j){ int tmp=A[i][j]; A[i][j]=A[j][i]; A[j][i]=tmp; } } return A; }else{ vector<vector<int>> B(n,vector<int>(m,0)); for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ B[i][j]=A[j][i]; } } return B; } } };
python解法
class Solution(object): def transpose(self, A): """ :type A: List[List[int]] :rtype: List[List[int]] """ return zip(*A)