rotate 2d array

Solutions on MaxInterview for rotate 2d array by the best coders in the world

showing results for - "rotate 2d array"
Jonah
04 May 2020
1rotated = list(zip(*original[::-1]))
2
Léandre
30 Nov 2020
1#clockwise:
2rotated = list(zip(*original[::-1]))
3#counterclockwise:
4rotated_ccw = list(zip(*original))[::-1]
Abdul
28 Aug 2020
1// For a nxn Matrix
2//  1 2 3      7 4 1
3//  4 5 6  ->  8 5 2
4//  7 8 9      9 6 3
5
6function rotadeMatrix( matrix ) {
7  var l = matrix.length-1;
8  for ( let x = 0; x < matrix.length / 2; x++ ) { 
9    for ( let y = x; y < l-x; y++ ) { 
10
11      let temp         = matrix[l-y][x  ]; 
12      matrix[l-y][x  ] = matrix[l-x][l-y]; 
13      matrix[l-x][l-y] = matrix[y  ][l-x]; 
14      matrix[y  ][l-x] = matrix[x  ][y  ]; 
15      matrix[x  ][y  ] = temp;
16    }
17  }
18  return matrix;
19}