题解 | Java #输入n个整数,输出其中最小的k个#

输入n个整数,输出其中最小的k个

https://www.nowcoder.com/practice/69ef2267aafd4d52b250a272fd27052c

排序题首先想到通过TreeSet实现自动排序,通过Iterator遍历器实现集合遍历

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        TreeSet<Integer> ts = new TreeSet<>();
        int size = sc.nextInt();
        int position = sc.nextInt();
        if (size < position){
            System.out.println("Error");
        }else{
            while (sc.hasNext()){
                int input = sc.nextInt();
                ts.add(input);
            }
            Iterator<Integer> it = ts.iterator();
            for (int i = 0; i < position; i++){
                System.out.print(it.next() + " ");
            }
        }
    }
}

全部评论

相关推荐

08-29 19:44
门头沟学院 Java
wish233:很强了,加上这个学历,一点小建议就是可以把你的项目做一个压测,比如说你的RPC,可以对比一下和http相比性能怎么样。还有你的秒杀,一定要自己压测一遍,确保不会出现数据不一致的问题。还有就是对于分布式锁的粒度一定要理清楚,这个很容易被问到。
听劝,我这个简历该怎么改...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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