-
Notifications
You must be signed in to change notification settings - Fork 20
/
TransposeMatrix.js
64 lines (57 loc) · 1.41 KB
/
TransposeMatrix.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* Given a matrix A, return the transpose of A.
* <p>
* The transpose of a matrix is the matrix flipped over it's main diagonal, switching the row and column indices of the matrix.
* <p>
* Example 1:
* <p>
* Input: [[1,2,3],[4,5,6],[7,8,9]]
* Output: [[1,4,7],[2,5,8],[3,6,9]]
* Example 2:
* <p>
* Input: [[1,2,3],[4,5,6]]
* Output: [[1,4],[2,5],[3,6]]
* <p>
* Note:
* <p>
* 1 <= A.length <= 1000
* 1 <= A[0].length <= 1000
* <p>
* Result: Accepted.
*/
/**
* @param {number[][]} A
* @return {number[][]}
*/
let transpose = function (A) {
let tmp = new Array(A[0].length);
for (let i = 0; i < tmp.length; i++) {
tmp[i] = new Array(A.length);
}
for (let i = 0; i < A[0].length; i++) {
for (let j = 0; j < A.length; j++) {
tmp[i][j] = A[j][i];
}
}
return tmp;
};
if (transpose([[1, 1]]).toString() === [[1], [1]].toString()) {
console.log("pass");
} else {
console.error("failed");
}
if (transpose([[1], [1]]).toString() === [[1, 1]].toString()) {
console.log("pass");
} else {
console.error("failed");
}
if (transpose([[1, 2, 3], [4, 5, 6], [7, 8, 9]]).toString() === [[1, 4, 7], [2, 5, 8], [3, 6, 9]].toString()) {
console.log("pass");
} else {
console.error("failed");
}
if (transpose([[1, 2, 3], [4, 5, 6]]).toString() === [[1, 4], [2, 5], [3, 6]].toString()) {
console.log("pass");
} else {
console.error("failed");
}