题解 | #数据分类处理#

数据分类处理

http://www.nowcoder.com/practice/9a763ed59c7243bd8ab706b2da52b7fd

理解题意较难,代码不算难


import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int I = sc.nextInt();
            int[] arrI = new int[I];//序列I
            for (int i = 0; i < I; i++) {
                arrI[i] = sc.nextInt();
            }
            int r = sc.nextInt();
            TreeSet<Integer> treeSet = new TreeSet<>();//序列R,对R序列进行排序去重
            for (int i = 0; i < r; i++) {
                treeSet.add(sc.nextInt());
            }
            ArrayList<Integer> list = new ArrayList<>();//保存最终结果
            for (Integer integer : treeSet) { //查找是否有满足条件的I
                int count = 0;//统计满足个数
                ArrayList<Integer> temp = new ArrayList<>();//临时列表,保存索引,以及对应值
                for (int i = 0; i < arrI.length; i++) {
                    String str = String.valueOf(arrI[i]);//I整数对应的数字需要连续包含R<i>对应的数字
                    if(str.contains(String.valueOf(integer))){ //只要有一个满足条件就保存R<i>,保存个数,索引,具体I
                        count++;
                        temp.add(i);
                        temp.add(arrI[i]);
                    }
                }
                if(count>0){ //按顺序保存对应数据
                    list.add(integer);
                    list.add(count);
                    list.addAll(temp);
                }
            }
            //统计序列总个数
            list.add(0, list.size());
            //输出结果
            for (Integer integer : list) {
                System.out.print(integer+" ");
            }
        }
    }
}


全部评论

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务