-
Notifications
You must be signed in to change notification settings - Fork 24
/
0867-transpose-matrix.js
70 lines (59 loc) · 1.46 KB
/
0867-transpose-matrix.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
65
66
67
68
69
70
// 867. Transpose Matrix
// Easy 63%
// Given a matrix A, return the transpose of A.
// The transpose of a matrix is the matrix flipped over it's main diagonal,
// switching the row and column indices of the 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
/**
* @param {number[][]} A
* @return {number[][]}
*/
const transpose = function(A) {
const N = A.length, M = A[0].length
const res = []
for (let i = 0; i < M; i++) {
res[i] = []
for (let j = 0; j < N; j++) {
res[i].push(A[j][i])
}
}
return res
}
const inPlace = function(A) {
const N = A.length, M = A[0].length
const MAX = Math.max(N, M)
for (let i = N; i < MAX; i++) A.push([])
for (let i = 0; i < MAX; i++) {
for (let j = i; j < MAX; j++) {
const t = A[i][j]
A[i][j] = A[j][i]
A[j][i] = t
}
for (let j = N; j < MAX; j++) A[i].pop()
}
for (let i = M; i < MAX; i++) A.pop()
return A
}
;[
[[1,2,3],[4,5,6],[7,8,9]], // [[1,4,7],[2,5,8],[3,6,9]]
[[1,2,3],[4,5,6]], // [[1,4],[2,5],[3,6]]
[[1,2],[3,4],[5,6]],// [[1,3,5],[2,4,6]]
].forEach((A) => {
console.log(transpose(A))
})
// Solution:
// 1. 新建一个数组
// 2. 就地替换
// 先添加翻转后缺少的行
// 交换
// 去除列中多余的数
// 去除多余的行
// Submission Result: Accepted