aboutsummaryrefslogtreecommitdiff
path: root/transpose_matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'transpose_matrix.c')
-rw-r--r--transpose_matrix.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/transpose_matrix.c b/transpose_matrix.c
new file mode 100644
index 0000000..61e5d54
--- /dev/null
+++ b/transpose_matrix.c
@@ -0,0 +1,20 @@
+// 867. Transpose Matrix
+#include "leetcode.h"
+
+/*
+ * given a 2d integer array 'matrix', return the transpose of 'matrix'. the
+ * transpose of a matrix is the matrix flipped over its main diagonal switching
+ * the matrix's rows and column indices
+ */
+
+int **transpose(int **A, int ARowSize, int const *AColSizes, int **columnSizes,
+ int *returnSize) {
+ int **ans = malloc(sizeof(int *) * (*returnSize = *AColSizes));
+ *columnSizes = malloc(sizeof(int) * (*AColSizes));
+ for (int i = 0; i < (*AColSizes); ++i) {
+ ans[i] = malloc(sizeof(int) * (*(*columnSizes + i) = ARowSize));
+ for (int j = 0; j < ARowSize; ++j)
+ ans[i][j] = A[j][i];
+ }
+ return ans;
+}