题解 | #明明的随机数#
明明的随机数
https://www.nowcoder.com/practice/3245215fffb84b7b81285493eae92ff0
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 tokens = []; while(line = await readline()){ tokens.push(Number(line.split(' ')));//字符变成数字 } tokens = tokens.slice(1);//去除第一项 tokens.sort(function(a,b){return a-b});//升序排列 let mySet = new Set(tokens);//set中的元素不重复,可以用于去除重复项 console.log([...mySet].join('\n'));//输出的时候添加换行符 }()
我的方法
Number()
.slice()
.sort()
sortfunctiond的返回值必须是function(a,b){return a-b}的返回值,依据a-b或者b-a的值的正负决定升序还是降序,a-b是升序,b-a降序。
set
//数组去重 var mySet = new Set([1, 2, 3, 4, 4]); [...mySet]; // [1, 2, 3, 4] //并集 var a = new Set([1, 2, 3]); var b = new Set([4, 3, 2]); var union = new Set([...a, ...b]); // {1, 2, 3, 4} //交集 var a = new Set([1, 2, 3]); var b = new Set([4, 3, 2]); var intersect = new Set([...a].filter(x => b.has(x))); // {2, 3} //差集 var a = new Set([1, 2, 3]); var b = new Set([4, 3, 2]); var difference = new Set([...a].filter(x => !b.has(x))); // {1}
...扩展运算符
//扩展运算符(...)是ES6的语法,用于取出参数对象的所有可遍历属性,然后拷贝到当前对象之中。 //基本用法 let person = {name: "Amy", age: 15} let someone = { ...person } someone // {name: "Amy", age: 15} //特殊用法 //数组 let foo = { ...['a', 'b', 'c'] }; foo // {0: "a", 1: "b", 2: "c"} //空对象 let a = {...{}, a: 1} a // { a: 1 } //Int类型、Boolen类型、undefined、null 如果扩展运算符后面是上面这几种类型,都会返回一个空对象,因为它们没有自身属性。 // 等同于 {...Object(1)} {...1} // {} // 等同于 {...Object(true)} {...true} // {} // 等同于 {...Object(undefined)} {...undefined} // {} // 等同于 {...Object(null)} {...null} // {} //字符串 如果扩展运算符后面是字符串,它会自动转成一个类似数组的对象 {...'hello'} // {0: "h", 1: "e", 2: "l", 3: "l", 4: "o"} //对象的合并 let age = {age: 15} let name = {name: "Amy"} let person = {...age, ...name} person; // {age: 15, name: "Amy"} //注意事项 自定义的属性和拓展运算符对象里面属性的相同的时候: //自定义的属性在拓展运算符后面,则拓展运算符对象内部同名的属性将被覆盖掉。 let person = {name: "Amy", age: 15}; let someone = { ...person, name: "Mike", age: 17}; someone; //{name: "Mike", age: 17} //自定义的属性在拓展运算度前面,则变成设置新对象默认属性值。 let person = {name: "Amy", age: 15}; let someone = {name: "Mike", age: 17, ...person}; someone; //{name: "Amy", age: 15}
.join()
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 const arr = []; while ((line = await readline())) { let tokens = line.split(" "); let num = tokens[0]; let hasNum = arr.some((i) => i == num);//对arr中的每一项如果有i==num,返回true。 //i是arr中的每一项 if (!hasNum || arr.length == 0) { arr.push(num); } } arr.sort((a, b) => a - b);//箭头函数真的好妙 arr.forEach((i) => { console.log(i);//循环中cosole.log()会自动换行 }); })();
牛客990644号——的方法
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 const arr = [] while(line = await readline()){ arr.push(parseInt(line))//字符串变成数字,再push给arr } arr.shift() let arr1 = [...new Set(arr)] // arr1.sort((a, b) => a-b); for(let i = 1; i < arr1.length; i++){ let j = i let temp = arr1[i] while(j > 0 && temp < arr1[j-1] ) { arr1[j] = arr1[j-1] j-- } arr1[j] = temp } arr1.forEach(item => { console.log(item) }) }()
{&}——的方法
parseInt()
.shift()
const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); let count = 0; let numsArr = []; let map = {}; let resultArr = []; rl.on("line", function (line) { if (count === 0) { count = parseInt(line, 10); } else { count -= 1; numsArr.push(parseInt(line, 10)); }//numsArr=[2,2,1] if (count === 0) { numsArr.sort((a, b) => a - b); for (let k = 0; k < numsArr.length; k++) { let item = numsArr[k]; if (!map[item]) {//判断map对象中是否有item元素,没有的话是undefine。 resultArr.push(item); } map[item] = 1;//让map对象中的item标记为1,代表map中已经有item了 } for (let i = 0; i < resultArr.length; i++) { console.log(resultArr[i]); } } });
安仁斗辣椒——的方法
const rl = require("readline").createInterface({ input: process.stdin }); let input_arr = []; let norepeat_arr = []; rl.on("line", function(line) { input_arr.push(line); }) rl.on("close", () => { if (input_arr[0] == input_arr.length - 1) { // 删除 input_arr.shift(); for (let i = 0; i < input_arr.length; i++) { if (norepeat_arr.indexOf(input_arr[i]) == -1) {//用indexOf判断是否存在,好秒 norepeat_arr.push(input_arr[i]); } } norepeat_arr.sort((a, b) => { return a - b; }) norepeat_arr.forEach(item => { console.log(item); }) } })
֟甜橙🍊——的方法
练练练练练 文章被收录于专栏
练练练练练