我正在尝试编写一个解决方案,返回字符串中最常见的字母,以及该字母出现的次数。 我想出的解决方案是循环遍历字符串,一次将一个字母保存在一个名为 letter 的变量中,并将所有其他字母与此进行比较。然后一旦字符串结束,循环将保持第二个字母并重复。
我遇到的问题是它每次都只返回字符串中的第一个字母。它得到正确的匹配数,但忽略了其他字母有更多匹配的事实。
我做错了什么?
var match = 0;
var matchCount = 0;
var letter = 0;
var count = 0;
indx1 = 0;
indx2 = 0;
function problemTen(a) {
while (indx1 < a.length) {
letter = a[indx1]; //hold the first letter in the string in the letter variable
while (indx2 < a.length) { //now rotate through every other letter to compare
if (a[indx2] == letter) { //compare
count += 1; // if there is a match, add one to the count variable to hold the number of matches
}
indx2 += 1; //cycle through the rest of the string
}
if (matchCount === 0 || count > matchCount) { // if it’s the first time around, or if this letter had more matches than the previous letter
match = letter; // hold the letter in the match variable
matchCount = count; //hold the number of matches in the count variable
}
indx1 += 1; //cycle through the first variable that you compare
}
return [match, matchCount]; //return results
}
console.log(problemTen("astrings"));
编辑:这是我想出的解决方案。
function problemTen (a) {
var match = 0;
var matchCount = 0;
var letter;
for(var indx1 = 0; indx1<a.length; indx1++) {
letter = a[indx1];
var count = 1;
for(var indx2 = indx1 + 1; indx2<a.length; indx2++) {
if(a[indx2] == letter) {
count +=1;
}
}
if(matchCount === 0 || count>matchCount) {
match = letter;
matchCount =count;
}
}
return [match, matchCount];
}
console.log(problemTen("iamatinystring"));
最佳答案
也许检查这个解决方案,它的时间复杂度与你试图实现的那个相比是线性的,它是四倍的:
function problemTen(a) {
var array = a.split('');
var max = 0;
var letter;
var counter = array.reduce(function(memo, item) {
memo[item] = memo[item] ? memo[item] + 1 : 1;
if (memo[item] > max) {
max = memo[item];
letter = item;
}
return memo;
}, {});
return [letter, max];
}
console.log(problemTen("astrings"));
如果你想坚持你的解决方案,问题是,当你再次运行外部 while 循环时,你并没有重置 indx2 和 count 变量,因此,indx2 = a.length 已经存在,而内部 while循环不再运行来计算字母。添加这些重置后,您的代码应该是这样的:
function problemTen(a) {
var match = 0;
var matchCount = 0
var letter = 0;
var count = 0;
indx1 = 0;
indx2 = 0;
while (indx1 < a.length) {
letter = a[indx1]; //hold the first letter in the string in the letter variable
while (indx2 < a.length) { //now rotate through every other letter to compare
if (a[indx2] === letter) { //compare
count += 1; // if there is a match, add one to the count variable to hold the number of matches
}
indx2 += 1; //cycle through the rest of the string
}
if (matchCount === 0 || count > matchCount) { // if it’s the first time around, or if this letter had more matches than the previous letter
match = letter; // hold the letter in the match variable
matchCount = count; //hold the number of matches in the count variable
}
indx1 += 1; //cycle through the first variable that you compare
// HERE WE RESET indx2 and match to 0
indx2 = 0;
match = 0;
}
return [match, matchCount]; //return results
}
console.log(problemTen("astrings"));
其实要达到O(n)的时间复杂度,不一定要用reduce,一个循环也可以,而且肯定会快很多,我只是更喜欢函数式编程,因为它的结果更好的可读性:
function problemTen(a) {
var max = 0;
var letter;
var counter = {};
for (var i = 0, l = a.length; i<l; i++) {
counter[a[i]] = counter[a[i]] ? counter[a[i]] + 1 : 1;
if (counter[a[i]] > max) {
max = counter[a[i]];
letter = a[i];
}
}
return [letter, max];
}
console.log(problemTen("astrings"));
希望这对您有所帮助。
关于javascript - 我的 javascript 代码返回最常见的字母有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34666918/