-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrotate-image.js
73 lines (65 loc) · 1.67 KB
/
rotate-image.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
71
72
73
/**
* Source: https://leetcode.com/problems/rotate-image/
* Tags: [Array]
* Level: Medium
* Title: Rotate Image
* Auther: @imcoddy
* Content: You are given an n x n 2D matrix representing an image.
* Rotate the image by 90 degrees (clockwise).
* Follow up:
* Could you do this in-place?
*/
/**
* @param {number[][]} matrix
* @return {void} Do not return anything, modify matrix in-place instead.
*/
/**
* Memo: for element matrix[i,j], the relation is below when rotated. A bit tricky it differs on whether n is even or odd.
* 0: [i,j]
* 90: [j,n-1-i]
* 180: [n-1-i, n-1-j]
* 270: [n-1-j,i]
* Runtime: 128ms
* Tests: 20 test cases passed
* Rank: S
*/
var rotate = function(matrix) {
var n = matrix.length;
for (var i = 0; i < ~~((n + 1) / 2); i++) {
for (var j = 0; j < ~~((n ) / 2); j++) {
var t = matrix[i][j];
matrix[i][j] = matrix[n - 1 - j][i];
matrix[n - 1 - j][i] = matrix[n - 1 - i][n - 1 - j];
matrix[n - 1 - i][n - 1 - j] = matrix[j][n - 1 - i];
matrix[j][n - 1 - i] = t;
}
}
};
function createMatrix(n) {
var matrix = [];
var count = 1;
for (var i = 0; i < n; i++) {
var a = [];
for (var j = 0; j < n; j++) {
a.push(count++);
}
matrix.push(a);
}
return matrix;
}
var matrix = createMatrix(2);
console.log(matrix);
rotate(matrix);
console.log(matrix);
matrix = createMatrix(3);
console.log(matrix);
rotate(matrix);
console.log(matrix);
matrix = createMatrix(4);
console.log(matrix);
rotate(matrix);
console.log(matrix);
matrix = createMatrix(5);
console.log(matrix);
rotate(matrix);
console.log(matrix);