File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ // This problem was asked by Facebook.
2
+
3
+ // Given an N by N matrix, rotate it by 90 degrees clockwise.
4
+
5
+ // For example, given the following matrix:
6
+
7
+ // [[1, 2, 3],
8
+ // [4, 5, 6],
9
+ // [7, 8, 9]]
10
+ // you should return:
11
+
12
+ // [[7, 4, 1],
13
+ // [8, 5, 2],
14
+ // [9, 6, 3]]
15
+ // Follow-up: What if you couldn't use any extra space?
16
+
17
+ main () {
18
+ List <List <int >> givenMatrix = [
19
+ [1 , 2 , 3 ],
20
+ [4 , 5 , 6 ],
21
+ [7 , 8 , 9 ]
22
+ ];
23
+
24
+ int N = givenMatrix.first.length;
25
+ print ("N: $N " );
26
+ print ("Initial matrix: ${givenMatrix }" );
27
+
28
+ for (int i = 0 ; i < givenMatrix.length; i++ ) {
29
+ for (int j = 0 ; j < givenMatrix.length; j++ ) {
30
+ givenMatrix[j].add (givenMatrix[i][j]);
31
+ }
32
+ }
33
+
34
+ for (int i = 0 ; i < givenMatrix.length; i++ ) {
35
+ givenMatrix[i].removeRange (0 , N );
36
+ givenMatrix[i] = givenMatrix[i].reversed.toList ();
37
+ }
38
+ print ("rotated matrix: ${givenMatrix }" );
39
+ }
You can’t perform that action at this time.
0 commit comments