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

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

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

运用了java8的boxed()流处理。

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

public class Main {

    public static void main(String[] arg) {
        Scanner scan = new Scanner(System.in);
        StringBuilder builder = new StringBuilder();
        while (scan.hasNext()) {
            scan.nextLine();
            String[] input = scan.nextLine().split(" ");
            int[] inputArr = Arrays.stream(input).mapToInt(Integer::parseInt).toArray();
            List<Integer> inputList = Arrays.stream(inputArr).boxed().collect(Collectors.toList());
            int orderType = scan.nextInt();
            if(orderType==0){
                Collections.sort(inputList);
            }else {
                Collections.sort(inputList,Collections.reverseOrder());
            }
            for(int i=0;i<inputList.size();i++) {
                if(i==inputList.size()-1){
                    builder.append(inputList.get(i));
                }else {
                    builder.append(inputList.get(i) + " ");
                }
            }
        }

        System.out.println(builder);
    }
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
昨天 18:02
好不容易拿到了字节Offer,鼠鼠做后端的,但家里人觉得可能被裁员不稳定,让鼠鼠去投国企,现在好纠结到底该咋选
文档传偷助手:该投就投吧,不过建议别放弃offer 拿到手里的才是最好的
投递字节跳动等公司9个岗位
点赞 评论 收藏
分享
asdasdasda...:19岁,不容易啊可能升个本会好点,现在学历歧视太严重了
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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