建设银行梅州分行网站,淘客网站建设要求,合肥网页设计就业,wordpress获取文章列表分页文章目录题目描述输入描述输出描述#xff1a;示例Java 代码实现题目描述
运维工程师采集到某产品线网运行一天产生的日志n条#xff0c;现需根据日志时间先后顺序对日志进行排序#xff0c;日志时间格式为H:M:S.N。 H表示小时(0~23) M表示分钟(0~59) S表示秒(0~59) N表…
文章目录题目描述输入描述输出描述示例Java 代码实现题目描述
运维工程师采集到某产品线网运行一天产生的日志n条现需根据日志时间先后顺序对日志进行排序日志时间格式为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表示日志条数1n100000接下来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
输入222:41:08.02322:41:08.23输出22:41:08.02322:41:08.23Java 代码实现
按照日志内容分析并定义对应的日志内容类 LogBody 。 按照题目要求对日志时间排序这里写了两种写法略有差异。 一种写法是使用 TreeSet 和比较器进行排序需要注意的是 Set本身是自带去重的在定义比较器时需要排除相等的情况。 另一种写法是使用集合类本身的 sort 方法以及比较器进行排序因为这里使用的是 list 不会去重因此不做特殊处理。
package com.example;import lombok.Data;import java.util.*;/*** 华为OD机试-运维日志排序** version v1.0* author: fengjinsong* date: 2023年02月25日 14时58分*/
public class LogSortDemo {static Scanner input new Scanner(System.in);public static void main(String[] args) {// 使用比较器将日志体安装时间排序针对于set而言没有相等的情况会自动去重ComparatorLogBody comparatorByLogTimeForSet (o1, o2) -(o1.millSeconds o1.seconds * 1000 o1.minutes * 60 * 1000 o1.hours * 60 * 60 * 1000)- (o2.millSeconds o2.seconds * 1000 o2.minutes * 60 * 1000 o2.hours * 60 * 60 * 1000) 0 ? 1 : -1;// 使用比较器将日志体安装时间排序针对于list而言有相等的情况但是不会自动去重ComparatorLogBody comparatorByLogTimeForList Comparator.comparingInt(o - (o.millSeconds o.seconds * 1000 o.minutes * 60 * 1000 o.hours * 60 * 60 * 1000));TreeSetLogBody treeSet new TreeSet(comparatorByLogTimeForSet);ListLogBody logBodyList new ArrayList();System.out.println(输入日志条数);// 日志条数for (int logCount input.nextInt(); logCount 0; logCount--) {System.out.println(输入日志);String log input.next();// 将日志按照 小时:分钟:秒.毫秒 的格式进行切分String[] logSplit log.split(:);LogBody logBody new LogBody(logSplit[0], logSplit[1], logSplit[2], log);treeSet.add(logBody);logBodyList.add(logBody);}System.out.println(set排序);treeSet.forEach(log - System.out.println(log.sourceLog));System.out.println(list排序);logBodyList.sort(comparatorByLogTimeForList);logBodyList.forEach(log - System.out.println(log.sourceLog));}Dataprivate static class LogBody {public LogBody(String hours, String minutes, String secondsAndMillSeconds, String sourceLog) {this.hours Integer.parseInt(hours);this.minutes Integer.parseInt(minutes);String[] secondsBody secondsAndMillSeconds.split(\\.);this.seconds Integer.parseInt(secondsBody[0]);this.millSeconds Integer.parseInt(secondsBody[1]);this.sourceLog sourceLog;}private int hours;private int minutes;private int seconds;private int millSeconds;private String sourceLog;}
}