Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

122
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda