题解 | #数据分类处理#

数据分类处理

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

问题梳理

  • 找出 I 中包含 R[i] 的所有元素;
  • 存在, 则输出 R[i], I 中满足条件的元素个数, 元素索引, 元素值;
  • 对 R[i] 进行排序(从小到大), 并去重;
  • 需要序列起始位置 统计并输出后序元素个数;

思路

  1. 借助 TreeSet 对 R[i] 进行去重和排序;
  2. I 放入数组就行;
  3. 遍历 I 并与 R[i] 进行比较, 封装 Found, 用于记录比较结果 {R[i], iIndexs};
  4. 将比较结果放入 foundList, 以便保持顺序和统计数量;
  5. 将 foundList 按指定格式进行输出;

代码实现

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {

    // 封装查找结果
    public static class Found {
        public String r;
        public ArrayList<Integer> iIndexs;

        public Found(String r, ArrayList<Integer> iIndexs) {
            this.r = r;
            this.iIndexs = iIndexs;
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String iLine = in.nextLine();
        String rLine = in.nextLine();
        String[] I = iLine.substring(iLine.indexOf(" ") + 1).split(" ");
        String[] R = rLine.substring(rLine.indexOf(" ") + 1).split(" ");
        // R 排序并去重
        TreeSet<Integer> RTree = new TreeSet();
        for (int i = 0; i < R.length; i++) RTree.add(Integer.valueOf(R[i]));
        // 遍历寻找符合条件的 I, 存入 foundList
        Iterator RI = RTree.iterator();
        ArrayList<Found> foundList = new ArrayList();
        while (RI.hasNext()) {
            String rStr = String.valueOf(RI.next());
            ArrayList<Integer> iIndexs = new ArrayList();
            for (int j = 0; j < I.length; j++) {
                if (I[j].contains(rStr)) iIndexs.add(j);
            }
            if (iIndexs.size() > 0) foundList.add(new Found(rStr, iIndexs));
        }
        // 寻找完成, 按指定格式输出 foundList
        LinkedList<String> outputItems = new LinkedList();
        for (int k = 0; k < foundList.size(); k++) {
            Found found = foundList.get(k);
            outputItems.add(found.r);
            outputItems.add(String.valueOf(found.iIndexs.size()));
            for (Integer index : found.iIndexs) {
                outputItems.add(String.valueOf(index));
                outputItems.add(I[index]);
            }
        }
        // 头部添加元素个数
        outputItems.addFirst(String.valueOf(outputItems.size()));
        System.out.println(String.join(" ", outputItems));
    }
}

全部评论

相关推荐

07-29 14:57
门头沟学院 Java
复盘中的钝角很想踢足...:别说ppt了,我简历去面试都不带的
点赞 评论 收藏
分享
这个状态都快维持十天了
投递小鹏汽车等公司10个岗位
点赞 评论 收藏
分享
Vincent777...:实习经历可以考虑放上去,对于软件使用方面可以细化一些,比如调整为:熟悉基于LSDYNA的瞬态动力学仿真分析,熟悉基于WORKBENCH的结构拓扑优化
我的简历长这样
点赞 评论 收藏
分享
嗨害嗨我来了:你跟他说开迈巴赫呢,一个月好几万,让学弟尝尝一点小小的社会险恶
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务