题解 | #明明的随机数#
明明的随机数
https://www.nowcoder.com/practice/3245215fffb84b7b81285493eae92ff0
最大元素不到500,还要去重,这个明显是走的哈希的路子
直接一次遍历得到哈希,然后遍历哈希输出就行了。这个属于计数排序,复杂度O(n+k)
const rl = require("readline").createInterface({ input: process.stdin }); var iter = rl[Symbol.asyncIterator](); const readline = async () => (await iter.next()).value; void async function () { // Write your code here let input = []; let t = false; while(line = await readline()){ let tokens = line.split(' '); if(t)input.push(parseInt(tokens[0])); t=true; } let hash = new Array(500).fill(0); for(let i=0;i<input.length;i++){ hash[input[i]]++;//可拓展到保留 } for(let i=0;i<500;i++){ if(hash[i])console.log(i); } }()