题解 | #输入整型数组和排序标识,对其元素进行排序#

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

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

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            String num = in.nextLine();
            String arr = in.nextLine();
            final String rule = in.nextLine();

            String[] arrs = arr.split(" ");
            List<Integer> a = new ArrayList<>();
            for (String s : arrs) {
                a.add(Integer.valueOf(s));
            }
            //System.out.println(a);

            Collections.sort(a, new Comparator<Integer>() {
                @Override
                public int compare(Integer o1, Integer o2) {
                    if ("0".equals(rule))return o1 - o2;
                    return o2 - o1;
                }

            });
            System.out.println(a.toString().replaceAll(", ", " ").replace("[",
                               "").replace("]", ""));



        }
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-04 18:02
好不容易拿到了字节Offer,鼠鼠做后端的,但家里人觉得可能被裁员不稳定,让鼠鼠去投国企,现在好纠结到底该咋选
文档传偷助手:该投就投吧,不过建议别放弃offer 拿到手里的才是最好的
投递字节跳动等公司9个岗位
点赞 评论 收藏
分享
05-22 09:23
门头沟学院 Java
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-02 15:39
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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