题解 | #数据分类处理#
数据分类处理
https://www.nowcoder.com/practice/9a763ed59c7243bd8ab706b2da52b7fd
import java.awt.List; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.Scanner; import java.util.TreeSet; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int first = in.nextInt(); int[] hs = new int[first]; for (int i = 0; i < first; i++) { hs[i] = in.nextInt(); } int second = in.nextInt(); TreeSet<Integer> ts = new TreeSet<>(); for (int i = 0; i < second; i++) { ts.add(in.nextInt()); } ArrayList<Integer> r = getOutput(hs, ts); System.out.print(r.size() + " "); for (int i : r) { System.out.print(i + " "); } } private static ArrayList<Integer> getOutput(int[] hsss, TreeSet<Integer> ts) { ArrayList<Integer> list = new ArrayList<>(); Iterator iterator = ts.iterator(); while (iterator.hasNext()) { String num = iterator.next().toString(); int tempIndex = list.size(); int tempC = 0; for (int k = 0; k < hsss.length; k++) { if (String.valueOf(hsss[k]).contains(num)) { list.add(k); list.add(hsss[k]); tempC++; } } if (tempC > 0) { list.add(tempIndex, tempC); list.add(tempIndex, Integer.parseInt(num)); } } return list; } }
- TreeSet 排序和去重
- 逻辑其实比较简单