题解 | #明明的随机数#
明明的随机数
https://www.nowcoder.com/practice/3245215fffb84b7b81285493eae92ff0
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// while (scan.hasNext()) {
// ArrayList<Integer> list = new ArrayList<>();
// int count = scan.nextInt();
// for (int i = 0; i < count; i++) {
// int num = scan.nextInt();
// if (!list.contains(num)) {
// list.add(num);
// }
// }
// Collections.sort(list);
// list.forEach(System.out::println);
// }
int count = scan.nextInt();
TreeSet<Integer> treeSet = new TreeSet<>();
for (int i = 0; i < count; i++) {
treeSet.add(scan.nextInt());
}
Iterator<Integer> iterator = treeSet.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
方法1:
使用list接收输入并去重,排序,输出;
方法2:
利用treeSet数据结构,去重排序,直接输出;
查看8道真题和解析

