Monday, August 17, 2015

Matrix Rotation by 90 degrees clockwise

 public int[][] rotate90(int[][] matrix)
 {
  
  int rowLength = matrix.length;
  int colLength = matrix[0].length;
  int [][] res = new int[colLength][rowLength];
  int col=0;
  for(int i=rowLength-1; i>= 0; i--, col++)
  {
   for(int j=0, row=0; j< colLength; j++, row++)
   {
    res[row][col] = matrix[i][j];
   }
  }
  return res;
 }

No comments:

Post a Comment