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

191
Views
El elemento de matriz `String` no es mutable

Estoy tratando de resolver 557 en leetCode con dos punteros y encontré un comportamiento extraño que no puedo explicar.

A continuación se muestra mi código

 /** * @param {string} s * @return {string} */ var reverseWords = function(s) { // return s.split(" ").map(w => w.split("").reverse().join("")).join(" ") let arr = s.split(" "), w for(let i = 0; i < arr.length; i++){ let x = arr[i], left = 0, right = x.length - 1, holder = null while(left < right){ holder = x[left] x[left] = x[right] x[right] = holder left += 1 right -= 1 } } return arr.join(" ") };

y ejemplo "Let's take LeetCode contest"

si bien puedo mover el ciclo while a una función y resolverlo de una manera diferente, no estoy seguro de por qué x no cambia, según tengo entendido, este es un elemento de matriz y se puede cambiar.

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

0

Esto sucede porque la variable "x" no es una matriz sino una cadena que proviene de la variable "arr" para que funcione, debe cambiar

 let x = arr[i]

a

 let x = arr[i].split("")

el codigo correcto es:

 var reverseWords = function(s) { // return s.split(" ").map(w => w.split("").reverse().join("")).join(" ") let arr = s.split(" "), w for(let i = 0; i < arr.length; i++){ let x = arr[i].split(""), left = 0, right = x.length - 1, holder = null while(left < right){ holder = x[right] x[right] = x[left] x[left] = holder left += 1 right -= 1 } arr[i] = x.join(""); } return arr.join(" ") };
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!