题解 | #输入整型数组和排序标识,按照升序或降序排序#

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

https://www.nowcoder.com/practice/dd0c6b26c9e541f5b935047ff4156309?tpId=37&tqId=21324&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3FjudgeStatus%3D3%26page%3D1%26pageSize%3D50%26search%3D%26tpId%3D37%26type%3D37&difficulty=undefined&judgeStatus=3&tags=&title=

效率为啥这么低?

package com.example.demo.simple;

import java.util.*;

/**
 * 【输入整型数组和排序标识,对其元素按照升序或降序进行排序】
 *
 */
public class Main_40 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int size = sc.nextInt();

        Integer[] ints = new Integer[size];
        for (int i = 0; i < size; i++) {
            ints[i] = sc.nextInt();
        }
        int sort = sc.nextInt();

        List<Integer> arrayList = new ArrayList<>(Arrays.asList(ints));
        if (sort == 0) {
            arrayList.sort((o1, o2) -> o1 - o2);
        } else {
            arrayList.sort((o1, o2) -> o2 - o1);
        }

        for (Integer integer : arrayList) {
            System.out.print(integer + " ");
        }
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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