题解 | #数据分类处理#
数据分类处理
https://www.nowcoder.com/practice/9a763ed59c7243bd8ab706b2da52b7fd
用TreeSet接收R序列(排序+不重复)
遍历TreeSet,找到I序列中包含R的元素
用LinkedList接收,便于在开头插入元素
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case // 有序,可以重复 ArrayList<Integer> I = new ArrayList<>(); // 要排序,不重复 TreeSet<Integer> R = new TreeSet<>(); int countI = in.nextInt(); for (int i = 0; i < countI; i++) { I.add(in.nextInt()); } int countR = in.nextInt(); for (int i = 0; i < countR; i++) { R.add(in.nextInt()); } LinkedList<Integer> result = new LinkedList<>(); for (int r : R) { int lengthStart = result.size(); // 记录当前result的长度 for (int i = 0; i < I.size(); i++) { if ((I.get(i) + "").contains(r+"")){ result.add(i); result.add(I.get(i)); } } int lengthEnd = (result.size()-lengthStart)/2; if(lengthEnd == 0){ continue; }else{ // 在上一个元素循环结束位置添加 result.add(lengthStart, lengthEnd); result.add(lengthStart, r); } } result.add(0,result.size()); for(int re:result){ System.out.print(re + " "); } } } }