标签:字母转 lse ret ima UNC src image rip arc
需要用到素数表,我们需要将字母转换成素数
var prime = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,
53,59,61,67,71,73,79,83, 89,97,101];
var groupAnagrams = function(strs) {
var hash = {}, ret = [], index = 0;
for(let word of strs){
var sum = 1;
for(let c of word){
var i = c.charCodeAt(0) - 97;
sum *= prime[ i ]
}
if( sum in hash){
ret [ hash[sum] ].push(word)
}else{
hash[sum] = index++
ret[ hash[sum] ] = [word]
}
}
return ret;
};
标签:字母转 lse ret ima UNC src image rip arc
原文地址:https://www.cnblogs.com/rubylouvre/p/12045968.html