美文网首页Leetcode
Leetcode 867. Transpose Matrix

Leetcode 867. Transpose Matrix

作者: SnailTyan | 来源:发表于2018-10-22 18:30 被阅读0次

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Transpose Matrix

2. Solution

class Solution {
public:
    vector<vector<int>> transpose(vector<vector<int>>& A) {
        vector<vector<int>> result;
        int rows = A.size();
        if(rows == 0) {
            return result;
        } 
        int columns = A[0].size();
        for(int i = 0; i < columns; i++) {
            vector<int> row;
            for(int j = 0; j < rows; j++) {
                row.emplace_back(A[j][i]);
            }
            result.emplace_back(row);
        }
        return result;
    }
};

Reference

  1. https://leetcode.com/problems/transpose-matrix/description/

相关文章

网友评论

    本文标题:Leetcode 867. Transpose Matrix

    本文链接:https://www.haomeiwen.com/subject/fangzftx.html