题解 | #明明的随机数#
明明的随机数
https://www.nowcoder.com/practice/3245215fffb84b7b81285493eae92ff0
三种解法
import java.util.*; public class Main { public static void main(String[] args) { solution1(); solution2(); solution3(); } static void solution1() { Scanner in = new Scanner(System.in); in.nextInt(); boolean[] arr = new boolean[501]; while (in.hasNextInt()) { arr[in.nextInt()] = true; } for (int i = 1; i < arr.length; i++) { if (arr[i]) { System.out.println(i); } } } static void solution2() { Scanner in = new Scanner(System.in); in.nextInt(); BitSet bitset = new BitSet(); Queue<Integer> pq = new PriorityQueue<Integer>(); while (in.hasNextInt()) { int val = in.nextInt(); if (!bitset.get(val)) { bitset.set(val); pq.offer(val); } } while (!pq.isEmpty()) { System.out.println(pq.poll()); } } static void solution3() { Scanner in = new Scanner(System.in); in.nextInt(); TreeSet set = new TreeSet(); while (in.hasNextInt()) { set.add(in.nextInt()); } set.forEach(System.out::println); } }#华为笔试#