我试图完成来自Leetcode的反转元音挑战,代码看起来没问题但不起作用。

huangapple go评论73阅读模式
英文:

im trying to do the reverse vowel challlange from leetcode, code looks good but its not working

问题

在第二个循环中,我正在检查是否满足条件 i === markedItemsIndex,但我的代码从未进入该条件,因此 'here' 永远不会被打印。

我已经检查了 imarkedItemsIndex 的数据类型,它们都是数字。你可以尝试以下方法来修复这个问题:

var reverseVowels = function (s) {
  // ... (你的其余代码保持不变)

  for (let i = 0; i < newString.length; i++) {
    if (i === markedItemsIndex[count]) {
      console.log('here'); // 这里的代码应该执行
      newString[i] = reversedMarkedItems[count];
      count++; // 在这里增加 count
    }
  }

  // ... (你的其余代码保持不变)
  
  return newString;
};

问题在于你在进入条件后没有增加 count 的值,导致你的代码无法正确处理所有标记的元音字符。通过在满足条件后增加 count,你应该能够修复这个问题。

英文:

with input 'hello', output should be 'holle'

heres my code,

var reverseVowels = function (s) {
  // change s into array
  // if a, i, u, e, o
  // change that item to &#39;marked&#39;
  // also put the index of that marked items into another array
  // put the marked items to another array
  // sort that marked items array, then reverse it
  // // im at here right now
  // then put that array inside its original place

  let newString = s.split(&#39;&#39;);
  let markedItems = [];
  let markedItemsIndex = [];
  let count = 0;

  for (let i = 0; i &lt; newString.length; i++) {
    if (
      newString[i] === &#39;a&#39; ||
      newString[i] === &#39;i&#39; ||
      newString[i] === &#39;u&#39; ||
      newString[i] === &#39;e&#39; ||
      newString[i] === &#39;o&#39;
    ) {
      markedItemsIndex.push(i);
      markedItems.push(newString[i]);
      newString[i] = &#39;marked&#39;;
    }
  }

  let reversedMarkedItems = markedItems.reverse();

  for (let i = 0; i &lt; newString.length; i++) {
    if (i === markedItemsIndex[count]) {
      console.log(&#39;here&#39;);
      newString[i] = reversedMarkedItems[count];
    }
    count++;
  }

  // return [newString, markedItems.reverse(), markedItemsIndex];
  return newString;
};

look at the second loop, im checking if the i === markedItemsIndex but my code never goes there, because of that, the 'here' never returns.

i've checked the type of i and type of markedItemsIndex, they're both a number. how can i fix this

答案1

得分: 1

只有在处理了该索引处的元音字母后才应该递增`count`即在if语句内部而不是在每次迭代中这会导致许多迭代被跳过)。

```js
if (i === markedItemsIndex[count]) {
    newString[i] = reversedMarkedItems[count];
    count++;
}

您还需要处理大写的元音字母,可以使用正则表达式更容易地进行检查。将第一个循环中的if语句更改为:

if (/[aeiou]/i.test(s[i])) {
    // 更新 markedItems...
}

最后,使用Array#join与空字符串来获取字符串作为结果。

return newString.join('');
英文:

You should only increment the count once the vowel at that index has been handled, i.e inside the if statement (not on every iteration, which causes a lot of them to be skipped).

if (i === markedItemsIndex[count]) {
    newString[i] = reversedMarkedItems[count];
    count++;
}

You also need to handle uppercase vowels as well, which can be checked more easily with a regular expression. Change the if statement in the first loop to:

if (/[aeiou]/i.test(s[i])) {
    // update markedItems...
}

Finally, use Array#join with an empty string to get a string as the result.

return newString.join(&#39;&#39;);

答案2

得分: 0

count++移到if内部:

var reverseVowels = function (s) {
  let newString = s.split('');
  let markedItems = [];
  let markedItemsIndex = [];
  let count = 0;

  for (let i = 0; i < newString.length; i++) {
    if (
      newString[i] === 'a' ||
      newString[i] === 'i' ||
      newString[i] === 'u' ||
      newString[i] === 'e' ||
      newString[i] === 'o'
    ) {
      markedItemsIndex.push(i);
      markedItems.push(newString[i]);
      newString[i] = 'marked';
      count++; // 将count++移到这里
    }
  }

  let reversedMarkedItems = markedItems.reverse();

  for (let i = 0; i < newString.length; i++) {
    if (i === markedItemsIndex[i]) {
      newString[i] = reversedMarkedItems[i];
    }
  }

  return newString.join(''); // 将数组连接成字符串
};

console.log(reverseVowels('hello')); // 输出: "holle"

这将使count在找到元音字符时递增。

英文:

Move the count++ inside the if:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var reverseVowels = function (s) {
  let newString = s.split(&#39;&#39;);
  let markedItems = [];
  let markedItemsIndex = [];
  let count = 0;

  for (let i = 0; i &lt; newString.length; i++) {
    if (
      newString[i] === &#39;a&#39; ||
      newString[i] === &#39;i&#39; ||
      newString[i] === &#39;u&#39; ||
      newString[i] === &#39;e&#39; ||
      newString[i] === &#39;o&#39;
    ) {
      markedItemsIndex.push(i);
      markedItems.push(newString[i]);
      newString[i] = &#39;marked&#39;;
    }
  }

  let reversedMarkedItems = markedItems.reverse();

  for (let i = 0; i &lt; newString.length; i++) {
    if (i === markedItemsIndex[count]) {
      newString[i] = reversedMarkedItems[count];
      count++;
    }
  }

  return newString.join(&#39;&#39;); // Join the array back into a string
};

console.log(reverseVowels(&#39;hello&#39;)); // Output: &quot;holle&quot;

<!-- end snippet -->

答案3

得分: 0

你可以使用 Array::unshift() 来以反向顺序收集已有的元音字母:

const reverseVowels = str => {
  const arr = [...str], set = new Set([...'aeiouAEIOU']), vowels =[], indices = [];
  arr.forEach((char,idx)=>set.has(char) && vowels.unshift(char) && indices.push(idx));
  indices.forEach((idx, i) => arr[idx] = vowels[i]);
  return arr.join('');
};

console.log(reverseVowels('hello'));
英文:

You could use Array::unshift() to collect the vowels already in a reversed order:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const reverseVowels = str =&gt; {
  const arr = [...str], set = new Set([...&#39;aeiouAEIOU&#39;]), vowels =[], indices = [];
  arr.forEach((char,idx)=&gt;set.has(char) &amp;&amp; vowels.unshift(char) &amp;&amp; indices.push(idx));
  indices.forEach((idx, i) =&gt; arr[idx] = vowels[i]);
  return arr.join(&#39;&#39;);
};

console.log(reverseVowels(&#39;hello&#39;));

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月17日 20:31:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76704478.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定