运维日志排序
描述
根据日志时间先后顺序对日志进行排序,日志时间格式为 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));
}
}
}