File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[][] } matrix
3
+ * @return {void } Do not return anything, modify matrix in-place instead.
4
+ */
5
+ var rotate = function ( matrix ) {
6
+ const rotateMatrix = ( matrix , start ) => {
7
+ const length = matrix . length ;
8
+
9
+ const end = length - 1 - start ;
10
+
11
+ if ( end <= start ) {
12
+ return ;
13
+ }
14
+
15
+ for ( let i = start ; i < end ; i ++ ) {
16
+ const temp = matrix [ start ] [ i ] ;
17
+
18
+ const target = length - 1 - i ;
19
+
20
+ matrix [ start ] [ i ] = matrix [ target ] [ start ] ;
21
+ matrix [ target ] [ start ] = matrix [ end ] [ target ] ;
22
+ matrix [ end ] [ target ] = matrix [ i ] [ end ] ;
23
+ matrix [ i ] [ end ] = temp ;
24
+ }
25
+
26
+ rotateMatrix ( matrix , start + 1 ) ;
27
+ }
28
+
29
+ rotateMatrix ( matrix , 0 ) ;
30
+ } ;
31
+
32
+ // 시간복잡도 O(n^2)
33
+ // 공간복잡도 O(l) -> 재귀호출 스택이 최대 매트릭스의 길이 / 2로 최대 쌓이므로
You can’t perform that action at this time.
0 commit comments