-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
57 lines (55 loc) · 986 Bytes
/
index.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
/**
* 思路:
* [
* [1,2,3],
* [4,5,6],
* [7,8,9]
* ]
*
* 先将矩形上下颠倒
*
* [
* [7,8,9],
* [4,5,6],
* [1,2,3],
* ]
*
* 然后沿着对角线交换
*
* [
* [7,4,1],
* [8,5,2],
* [9,6,3]
* ]
*
* @param {number[][]} matrix
* @return {void} Do not return anything, modify matrix in-place instead.
*/
var rotate = function(matrix) {
const result = matrix.reverse();
result.forEach((row, index) => {
for (let j = index + 1; j < row.length; j++) {
const tmp = result[index][j];
result[index][j] = result[j][index];
result[j][index] = tmp;
}
});
return result;
};
console.log(rotate([
[1,2,3],
[4,5,6],
[7,8,9]
]));
console.log(rotate([
[ 5, 1, 9,11],
[ 2, 4, 8,10],
[13, 3, 6, 7],
[15,14,12,16]
]));
module.exports = {
id:'48',
title:'Rotate Image',
url:'https://leetcode.com/problems/rotate-image/',
difficulty:'medium'
};