好吧,也许我最后一个问题我不够清楚。
我想在字符串中返回包含最多重复字符的单词。
以下字符串:
There/'s a passage that I got memorized, seems appropriate for this situation: Ezekiel 25,17.
会返回["appropriate"]
。
但是如果有多个单词具有相同数量的重复字符,我想将它们全部归还。所以以下字符串
Hello all from Boston
会返回["Hello", "all", "boston"]
这是我到目前为止的代码。此代码取自此另一个stackoverflow thread
function returnRepeatChar(str){
var maxCount = 0;
var word = '-1';
//split string into words based on spaces and count repeated characters
str.toLowerCase().split(" ").forEach(function(currentWord){
var hash = {};
//split word into characters and increment a hash map for repeated values
currentWord.split('').forEach(function(letter){
if (hash.hasOwnProperty(letter)){
hash[letter]++;
} else {
hash[letter] = 1;
}
});
//convert the hash map to an array of character counts
var characterCounts = Object.keys(hash).map(function(key){
return hash[key];
});
//find the maximum value in the squashed array
var currentMaxRepeatedCount = Math.max.apply(null, characterCounts);
//if the current word has a higher repeat count than previous max, replace it
if (currentMaxRepeatedCount > maxCount){
maxCount = currentMaxRepeatedCount;
word = currentWord;
}
});
return word;
}
console.log(returnRepeatChar("There/'s a passage that I got memorized, seems appropiate for this situation: Ezekiel 25,17.")); //"appropriate"

答案 0 :(得分:2)
对您的代码进行以下简单更改。
var word = [];
变为:
word
更新//if the current word has a higher repeat count than previous max, replace it
if (currentMaxRepeatedCount > maxCount){
maxCount = currentMaxRepeatedCount;
word = [currentWord];
} else if (currentMaxRepeatedCount == maxCount) {
// If it's the same as the max, add it to the list
word.push(currentWord);
}
的代码变为:
answer = 0
def recursion(x):
global answer
if( x > 10 ):
answer += 1
return recursion( x - 1)
return answer
recursion(15)
答案 1 :(得分:-1)
是的,你可以做到。尝试以下脚本。
strtotime()