题解 | #字符个数统计#
字符个数统计
https://www.nowcoder.com/practice/eb94f6a5b2ba49c6ac72d40b5ce95f50
const rl = require("readline").createInterface({ input: process.stdin }); var iter = rl[Symbol.asyncIterator](); const readline = async () => (await iter.next()).value; //方法一:借助哈希集合,set.size即为答案 void async function(){ const str = await readline(); const set = new Set(); for(const c of str){ set.add(c); } console.log(set.size); }() // 方法二:范围在(0~127,包括0和127)字符,所以可以用 Array(128).fill(0)来计数 void (async function () { const str = await readline(); const cnt = Array(128).fill(0); for (const c of str) { cnt[c.charCodeAt()]++; } console.log(cnt.filter(item=>item).length); })();
华为OD,我昵称“od400” 文章被收录于专栏
华为OD机试刷题历程,因涉及华为权益,牛客不允许更新了。我昵称“od400”,某博客继续更新。