連結串列的有序集合(Set用法)
阿新 • • 發佈:2019-02-02
連結串列的有序集合
Time Limit: 1000MS Memory Limit: 65536KBProblem Description
集合有一個重要的特性:互異性,即集合中任意兩個元素都是不同的,互異性使得集合中的元素沒有重複。給你 n 個包含重複數字的無序正整數序列,建立一個有序連結串列,連結串列中的結點按照數值非降序排列且不包含重複元素,輸出該有序連結串列。Input
輸入包含多組測試資料,對於每組測試資料: 輸入的第一行為一個正整數 n(1 ≤ n ≤ 100), 第二行為 n 個正整數 b1,b2,...,bn(0 ≤ bi ≤ 230)。Output
對於每組測試資料,按照非降序輸出連結串列的節點值。Example Input
1 2 2 1 1 6 6 3 5 2 2 3
Example Output
2 1 2 3 5 6
Hint
Author
qinchuan
import java.util.Scanner; import java.util.TreeSet; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while ( in.hasNext() ) { int n = in.nextInt(); int [] array = new int [n]; TreeSet<Integer> treeSet = new TreeSet<Integer>(); for ( int i = 0; i < n; i++ ) { array[i] = in.nextInt(); treeSet.add(array[i]); } int cnt = 1; for ( Integer k : treeSet ) { if ( cnt == treeSet.size() ) { System.out.println(k); } else { System.out.print(k+" "); } cnt++; } } in.close(); } }