FED32. 字符串字符统计
描述
统计字符串中每个字符的出现频率,返回一个 Object,key 为统计字符,value 为出现频率示例1
输入:
'hello world'
输出:
{h: 1, e: 1, l: 3, o: 2, w: 1, r: 1, d: 1}
HTML/CSS/JavaScript 解法, 执行用时: 855ms, 内存消耗: 77848KB, 提交时间: 2021-03-21
{"css":"","js":"function count(str) {\n var obj = {};\n for(var i = 0; i < str.length; i++){\n if (str[i] !== ' '){\n var key = str[i];\n if (obj[key] !== undefined){\n obj[key] = obj[key] + 1;\n } else{\n obj[key] = 1;\n }\n }\n }\n return obj;\n}","html":"","libs":[]}
HTML/CSS/JavaScript 解法, 执行用时: 859ms, 内存消耗: 77900KB, 提交时间: 2021-05-06
{"css":"","js":"function count(str) {\n var arr={};\n for(var i=0;i<str.length;i++){\n if(str[i]!==' '){\n var key=str[i];\n if(arr[key]!==undefined){\n arr[key]+=1;\n }else arr[key]=1; \n }\n }\n return arr;\n}","html":"","libs":[]}
HTML/CSS/JavaScript 解法, 执行用时: 859ms, 内存消耗: 77924KB, 提交时间: 2021-05-23
{"css":"","js":"function count(str) {\n let strArr = str.split('')\n let strObj = {}\n strArr.forEach(item=>{\n if(item!=''){\n if(strObj[item]) {\n strObj[item]=strObj[item]+1\n }else{\n strObj[item]=1\n }\n }\n \n })\n return strObj\n}","html":"","libs":[]}
HTML/CSS/JavaScript 解法, 执行用时: 860ms, 内存消耗: 77768KB, 提交时间: 2021-01-17
{"css":"","js":"function count(str) {\n let obj={};\n let len=str.length;\n for(let i=0;i<len;i++){\n if(str.charAt(i)!==''){\n if(obj[str.charAt(i)]){\n obj[str.charAt(i)]+=1\n }else{\n obj[str.charAt(i)]=1\n }\n \n }\n \n \n }\n return obj\n}\n\ncount('hello world')","html":"","libs":[]}
HTML/CSS/JavaScript 解法, 执行用时: 860ms, 内存消耗: 77772KB, 提交时间: 2021-07-20
{"css":"","js":"function count(str) {\n str = str.trim().split(' ');\n let tempA = '';\n for (let k in str) {\n tempA = tempA.concat(str[k])\n }\n let obj = {};\n for (let i = 0; i < tempA.length; i++) {\n let chars = tempA.charAt(i);//chars 是 字符串的每一个字符\n if (obj[chars]) {//o[chars]得到的是属性值\n //补全代码\n obj[chars] += 1;\n } else {\n //补全代码\n obj[chars] = 1;\n }\n }\n return obj;\n }","html":"","libs":[]}