题解 | 输入整型数组, 对其元素按照升序或降序进行排序

输入整型数组和排序标识,对其元素按照升序或降序进行排序

https://www.nowcoder.com/practice/dd0c6b26c9e541f5b935047ff4156309

const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void async function () {
    let arr = []
    
    while(line = await readline()){
       arr.push(line)  //[ '8', '1 2 4 9 3 55 64 25', '0' ]
    }
    arr.shift()
    let condition = arr.pop()
    arr = arr.join(' ').split(' ')
    arr.sort((a, b) => {
        if(parseInt(condition) ===  0){
            return parseInt(a) - parseInt(b)
        }else if(parseInt(condition) === 1){
            return parseInt(b) - parseInt(a)
        }
     })
    console.log(arr.join(' '))
}()

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务