输入包括两行: 第一行为序列长度n(1 ≤ n ≤ 50) 第二行为n个数sequence[i](1 ≤ sequence[i] ≤ 1000),以空格分隔
输出消除重复元素之后的序列,以空格分隔,行末无空格
9 100 100 100 99 99 99 100 100 100
99 100
//在加入list之前先做判断,如果没有出现过,则加入末尾,如果出现过,从list中删除这个数,在加入末尾 import java.util.ArrayList; import java.util.List; import java.util.Scanner; /** * Created by meichen on 17-9-7. */ public class Main { public static void main(String[] args){ Scanner in = new Scanner(System.in); while (in.hasNext()){ int num = in.nextInt(); int[] A = new int[num]; for(int i = 0; i < num; i++){ A[i] = in.nextInt(); } List<Integer> list = new ArrayList<Integer>(); for(int n : A){ if(!list.contains(n)){ list.add(n); }else { list.remove((Object)(n)); list.add(n); } } String str = ""; for(int i = 0; i < list.size() - 1; i++){ str += list.get(i) + " "; } str += list.get(list.size() - 1); System.out.print(str); } } }
import java.util.ArrayDeque; import java.util.Deque; import java.util.Scanner; /** *使用一个双端队列即可,比较简单,直接show the code 关键代码就五行 Deque<Integer> q = new ArrayDeque<>(); for (int i = n - 1; i >= 0; i --) { if (!q.contains(sequence[i])) { q.offerFirst(sequence[i]); } } */ public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] sequence = new int[n]; for (int i = 0; i < n; i ++) { sequence[i] = sc.nextInt(); } Deque<Integer> q = new ArrayDeque<>(); for (int i = n - 1; i >= 0; i --) { if (!q.contains(sequence[i])) { q.offerFirst(sequence[i]); } } int count = q.size(); for (int i = 0; i < count - 1; i ++) { System.out.print(q.poll() + " "); } System.out.println(q.poll()); } }
import java.util.*;
/**
Scanner scanner = new Scanner(System.in);
List<Integer> list = new ArrayList<Integer>();
List<Integer> list1 = new ArrayList<Integer>();
while (scanner.hasNext()){
Integer count = scanner.nextInt();
for (int i = 0; i < count; i++) {
Integer num = scanner.nextInt();
list.add(num);
}
for (int i = 0; i < count; i++) {
Integer temp = list.remove(0);
if (!list.contains(temp)){
list1.add(temp);
}
}
for (int i = 0; i < list1.size() - 1; i++) {
System.out.print(list1.get(i) + " ");
}
System.out.println(list1.get(list1.size()-1));
}
scanner.close();
} }
import java.util.*; public class Main { public static void main(String[] args) { Scanner scan=new Scanner(System.in); while(scan.hasNextLine()) { String num=scan.nextLine(); String str=scan.nextLine(); find(str); } } public static void find(String str) { String[] ch=str.split(" "); List<String> list=new ArrayList<String>(); for(int i=ch.length-1;i>=0;i--) { if(!list.contains(ch[i])) list.add(ch[i]); } for(int i=list.size()-1;i>=0;i--) { if(i!=0) System.out.print(list.get(i)+" "); else System.out.print(list.get(i)); } } }