← Home

867. Transpose Matrix

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
867

This is one of those problems where the clean idea matters more than the amount of code. For 867. Transpose Matrix, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

Guide

When?

This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The solution is organized around the main LeetCode entry point and a few local helpers.

Guide

Why?

The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.

  • The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(R * C), where R and C are the number of rows and columns in the given matrix A.
  • Space: O(R * C), the space used by the answer.

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01/**
02Given a matrix A, return the transpose of A.
03
04The transpose of a matrix is the matrix flipped over it's main diagonal, switching the row and column indices of the matrix.
05
06 
07
08Example 1:
09
10Input: [[1,2,3],[4,5,6],[7,8,9]]
11Output: [[1,4,7],[2,5,8],[3,6,9]]
12Example 2:
13
14Input: [[1,2,3],[4,5,6]]
15Output: [[1,4],[2,5],[3,6]]
16 
17
18Note:
19
201 <= A.length <= 1000
211 <= A[0].length <= 1000
22**/
23
24//Your runtime beats 35.18 % of cpp submissions.
25class Solution {
26public:
27    vector<vector<int>> transpose(vector<vector<int>>& A) {
28        vector<vector<int>> B(A[0].size(), vector<int>(A.size(),0));
29        for(int r = 0; r < A.size(); r++){
30            for(int c = 0; c < A[0].size(); c++){
31                B[c][r] = A[r][c];
32            }
33        }
34        return B;
35    }
36};
37
38/**
39Approach 1: Copy Directly
40Intuition and Algorithm
41
42The transpose of a matrix A with dimensions R x C is a matrix ans with dimensions C x R for which ans[c][r] = A[r][c].
43
44Let's initialize a new matrix ans representing the answer. Then, we'll copy each entry of the matrix as appropriate.
45
46Complexity Analysis
47
48Time Complexity: O(R * C), where R and C are the number of rows and columns in the given matrix A.
49
50Space Complexity: O(R * C), the space used by the answer. 
51**/
52
53//Your runtime beats 43.05 % of cpp submissions.
54class Solution {
55public:
56    vector<vector<int>> transpose(vector<vector<int>>& A) {
57        vector<int> tmp;
58        vector<vector<int>> B;
59        for(int r = 0; r < A[0].size(); r++){
60            tmp.clear();
61            for(int c = 0; c < A.size(); c++){
62                tmp.push_back(A[c][r]);
63            }
64            B.push_back(tmp);
65        }
66        return B;
67    }
68};

Cost

Complexity

Time
O(R * C), where R and C are the number of rows and columns in the given matrix A.
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(R * C), the space used by the answer.
Auxiliary state plus the answer structure where the problem requires one.