867. Transpose 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 <= A.length <= 1000
1 <= A[0].length <= 1000

  

解法:

创建B数组行数和列数刚好和A相反。

赋值也是与A的i和j相反。

 

代码参考:

 1 class Solution {
 2 public:
 3     vector<vector<int>> transpose(vector<vector<int>>& A) {
 4         int i=0, j=0;
 5         vector<vector<int>> B(A[0].size(),vector<int>(A.size(),0));
 6         for(i=0; i<A.size(); i++){
 7             for(j=0; j<A[0].size(); j++){
 8                 B[j][i]=A[i][j];
 9             }
10         }
11         return B;
12     }
13 };

 

posted @ 2020-05-17 14:31  habibah_chang  阅读(114)  评论(0编辑  收藏  举报