题解 | #简单错误记录#

简单错误记录

http://www.nowcoder.com/practice/2baa6aba39214d6ea91a2e03dff3fbeb

  • 使用一个arraylist存储顺序
  • 使用一个hashmap存储计数
  • 使用正则去除目录和超长文件名
  • 输出时,注意输入不足8个的情况
  • 注意终止输入的处理
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

/**
 * class com.sloera.nowcoder
 * user sloera
 * date 2022/2/24
 */
public class Main {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    // 注意 hasNext 和 hasNextLine 的区别
    final ArrayList<String> result = new ArrayList<>();
    final HashMap<String, Integer> count = new HashMap<>();
    while (in.hasNextLine()) { // 注意 while 处理多个 case
      final String s = in.nextLine();
      if (!"".equals(s)) {
        handle(result, count,s);
      } else {
        break;
      }
    }
    // 倒序输出8个
    for (int i = Math.max(result.size()-8, 0); i < result.size(); i++) {
      System.out.println(result.get(i) + " " + count.get(result.get(i)));
    }
  }

  private static void handle(ArrayList<String> result, HashMap<String, Integer> count, String s) {
    final String[] split = s.split("\\s+");
    // 替换目录路径 及 前16位
    String name  = split[0].replaceAll(".*\\\\", "").replaceAll("^.*?(.{16})$", "$1") + " " + split[1];
    if (count.containsKey(name)) {
      count.put(name, count.get(name) + 1);
    } else {
      result.add(name);
      count.put(name, 1);
    };
  }

}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-02 18:35
简历上把1个月实习写成了3个月,会进行背调吗?
码农索隆:一个月有一个月的实习经历,三个月有三个月的实习经历
点赞 评论 收藏
分享
积极的小学生不要香菜:你才沟通多少,没500不要说难
点赞 评论 收藏
分享
评论
2
2
分享

创作者周榜

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