Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

124
Vistas
its a n x n 2d matrix representing an image ,rotate the image by 90deg

its a n x n 2d matrix representing an image ,how to rotate the image by 90deg [[1,2,3],[4,5,6],[7,8,9]] to [[7,4,1],[8,5,2],[9,6,3]]?`

function rotate(matrix){
  
  for(let r=0;r<matrix.length;r++){
    
    for(let c=r;c<matrix[0].length;c++){
      
      
      [matrix[r][c],matrix[c][r]] = [matrix[c][r],matrix[r][c]]
    }
  }
  
  for(row of matrix){
    
    return row.reverse()
  }
  
}


console.log(rotate([[1,2,3],[4,5,6],[7,8,9]]))

the output is only showing [7,4,1]

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You have an unconditional return statement in a loop. That doesn't make sense. The function always returns the first row.reverse(). Move th return statement out of the loop:

function rotate(matrix){
  for(let r=0;r<matrix.length;r++){    
    for(let c=r;c<matrix[0].length;c++){        
      [matrix[r][c],matrix[c][r]] = [matrix[c][r],matrix[r][c]]
    }
  }
  
  for(row of matrix){
    row.reverse()
  }
  return matrix;
}

console.log(rotate([[1,2,3],[4,5,6],[7,8,9]]))

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda