英文:
im trying to do the reverse vowel challlange from leetcode, code looks good but its not working
问题
在第二个循环中,我正在检查是否满足条件 i === markedItemsIndex
,但我的代码从未进入该条件,因此 'here'
永远不会被打印。
我已经检查了 i
和 markedItemsIndex
的数据类型,它们都是数字。你可以尝试以下方法来修复这个问题:
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 'marked'
// 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('');
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';
}
}
let reversedMarkedItems = markedItems.reverse();
for (let i = 0; i < newString.length; i++) {
if (i === markedItemsIndex[count]) {
console.log('here');
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('');
答案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('');
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';
}
}
let reversedMarkedItems = markedItems.reverse();
for (let i = 0; i < newString.length; i++) {
if (i === markedItemsIndex[count]) {
newString[i] = reversedMarkedItems[count];
count++;
}
}
return newString.join(''); // Join the array back into a string
};
console.log(reverseVowels('hello')); // Output: "holle"
<!-- 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 => {
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'));
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论