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

106
Views
Problema de lógica en la lógica de matriz espiral de impresión

Traté de resolver el problema de la matriz espiral, pero me enfrento a algunos problemas con mi lógica. Código a continuación

 const arr = [ [1,2,3,4], [5,6,7,8], [9,10,11,12]] function spiralOrderMatrix(arr) { let top = 0; let left = 0; let right = arr[0].length - 1; let bottom = arr.length - 1; let result = []; while(top <= bottom && left <= right) { for(let i = left; i <= right; i++) { result.push(arr[top][i]); console.log(arr[top][i], '----top'); } top++; for(let j = top; j <= bottom; j++) { result.push(arr[j][right]); console.log(arr[j][right], '----Right'); } right--; for(let k = right; k >= left; k--) { result.push(arr[bottom][k]); console.log(arr[bottom][k], '----Bottom'); } bottom--; for(let l = bottom; l >= top; l--) { result.push(arr[l][left]); console.log(arr[l][left], '----left'); } left++; } console.log(result); } spiralOrderMatrix(arr);

salida esperada: [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7] salida real: [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7, 6]

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Para el empuje de resultados inferior e izquierdo, agregue si la condición para verificar la parte superior es menor que la parte inferior

 const arr = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12] ] function spiralOrderMatrix(arr) { let top = 0; let left = 0; let right = arr[0].length - 1; let bottom = arr.length - 1; let result = []; while (top <= bottom && left <= right) { for (let i = left; i <= right; i++) { result.push(arr[top][i]); console.log(arr[top][i], '----top'); } top++; for (let j = top; j <= bottom; j++) { result.push(arr[j][right]); console.log(arr[j][right], '----Right'); } right--; if (top <= bottom) { for (let k = right; k >= left; k--) { result.push(arr[bottom][k]); console.log(arr[bottom][k], '----Bottom'); } } bottom--; if (top <= bottom) { for (let l = bottom; l >= top; l--) { result.push(arr[l][left]); console.log(arr[l][left], '----left'); } } left++; } console.log(result); } spiralOrderMatrix(arr);

Referencia: https://gist.github.com/lienista/af013a6425eaf781cbc659474f0b91b6

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!