Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

131
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!