Skip to content

Commit f3a9009

Browse files
Day1269 done
1 parent f517d7e commit f3a9009

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
}

0 commit comments

Comments
 (0)