OD华为机试 5

运维日志排序

描述

根据日志时间先后顺序对日志进行排序,日志时间格式为 H:M:S.N
H 表示小时(0~23)
M 表示分钟(0~59)
S 表示秒(0~59)
N 表示毫秒(0~999)
时间可能并没有补全,也就是说,01:01:01.001 也可能表示为 1:1:1.1
第一行输入一个整数 n 表示日志条数,1<=n<=100000,接下来 n 行输入 n 个时间
按时间升序排序之后的时间,如果有两个时间表示的时间相同,则保持输入顺序

例一:

2
01:41:8.9
1:1:09.211
1:1:09.211
01:41:8.9

例二:

3
23:41:08.023
1:1:09.211
08:01:22.0
1:1:09.211
08:01:22.0
23:41:08.023

法一

import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args)throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int num = Integer.parseInt(br.readLine());
        String[] str = new String[num];
        for (int i = 0 ; i < str.length ; i++) {
            str[i] = br.readLine();
        }
        //构造一个可以升序排列的treemap
        Map<String, String> map = new TreeMap<>(new Comparator<String>() {
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
        //把比较的存在key,把真实要输出的存在value
        for (int i = 0 ; i < str.length ; i++) {
            String[] s = str[i].replace(".", ":").split(":");
            String res = "";
            if (s[0].length() == 1) {
                res += "0" + s[0];
            } else {
                res += s[0];
            }
            if (s[1].length() == 1) {
                res += "0" + s[1];
            } else {
                res += s[1];
            }
            if (s[2].length() == 1) {
                res += "0" + s[2];
            } else {
                res += s[2];
            }
            if (s[3].length() == 1) {
                res += "00" + s[3];
            } else if (s[3].length() == 2) {
                res += "0" + s[3];
            } else {
                res += s[3];
            }
            //相同时间,则按照顺序加入到map
            if (map.containsKey(res)) {
                map.put(res + 1, str[i]);
            } else {
                map.put(res, str[i]);
            }
        }
        //按照key来get,就输出value
        for (String ss : map.keySet()) {
            System.out.println(map.get(ss));
        }
    }
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值