Question part
topic | Log sorting |
difficulty | easy |
Question description | The operation and maintenance engineer collected N logs generated by a product running on the live network for one day, and now needs to sort the logs in chronological order according to the log time. The log time format is: H:M:S.N H represents hours (0-23), M represents minutes ( 0-59), S represents seconds (0-59), N represents milliseconds (0-999). The time may not be completed, that is, 01:01:01.001, or it may be expressed as 1:1:1.1. |
Enter description | Input an integer N in the first line, indicating the number of logs, 1 <= N <= 100000. Enter N times in the next N lines. |
Output description | After sorting the times in ascending order of time, if there are two times that represent the same time, the original order is maintained. |
Additional information | none |
------------------------------------------------------ | |
Example | |
Example 1 | |
enter | 2 01:41:8.9 1:1:09.211 |
output | 1:1:09.211 01:41:8.9 |
illustrate | none |
Example 2 | |
enter | 3 23:41:08.023 1:1:09.211 08:01:22.0 |
output | 1:1:09.211 08:01:22.0 23:41:08.023 |
illustrate | none |
Example 3 | |
enter | 2 22:41:08.023 22:41:08.23 |
output | 22:41:08.023 22:41:08.23 |
illustrate | The two times represent the same time and maintain their original output order. |
Interpretation and analysis
Item explanation:
Given a set of times, sort the times according to the established rules. If the two times are equal, keep the original order.
Analysis Yoshiro:
When comparing time, compare in three segments, first compare hours, then compare minutes, and finally compare seconds.
When comparing hours and minutes, note that the numbers may have leading zeros. When comparing minutes, you cannot directly compare the size of the decimal. It may contain two parts: seconds and milliseconds. Separate them by "." and then compare the seconds and milliseconds respectively by comparing the hours.
The time complexity depends on the sorting algorithm, which is O(nlogn), and the space complexity is O(n).
Code
Java code
import java.util.Arrays;
import java.util.Scanner;
import java.util.Comparator;
/**
* 日志排序
*
* @since 2023.11.01
* @version 0.1
* @author Frank
*
*/
public class LogSort {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String input = sc.nextLine();
int count = Integer.parseInt(input);
String[] timeArr = new String[count];
for (int i = 0; i < count; i++) {
input = sc.nextLine();
timeArr[i] = input;
}
processLogSort(timeArr);
}
}
private static void processLogSort(String[] timeArr) {
Comparator<String> comp = new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
String[] o1Arr = o1.split(":");
String[] o2Arr = o2.split(":");
int[] o1Int = new int[4];
int[] o2Int = new int[4];
for (int i = 0; i < 2; i++) {
o1Int[i] = Integer.parseInt(o1Arr[i]);
o2Int[i] = Integer.parseInt(o2Arr[i]);
}
// 此处是正则表达式
String[] second1Arr = o1Arr[2].split("\\.");
String[] second2Arr = o2Arr[2].split("\\.");
o1Int[2] = Integer.parseInt(second1Arr[0]);
o2Int[2] = Integer.parseInt(second2Arr[0]);
if (second1Arr.length == 2) {
o1Int[3] = Integer.parseInt(second1Arr[1]);
}
if (second2Arr.length == 2) {
o2Int[3] = Integer.parseInt(second2Arr[1]);
}
for (int i = 0; i < o1Int.length; i++) {
if (o1Int[i] == o2Int[i]) {
continue;
}
return o1Int[i] - o2Int[i];
}
return 0;
}
};
Arrays.sort(timeArr, comp);
for (int i = 0; i < timeArr.length; i++) {
System.out.println(timeArr[i]);
}
}
}
JavaScript code
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
void async function() {
while (line = await readline()) {
var count = parseInt(line);
var timeArr = new Array();
for (var i = 0; i < count; i++) {
line = await readline();
timeArr[i] = line;
}
processLogSort(timeArr);
}
}();
function processLogSort(timeArr) {
var comp = function(o1, o2) {
var o1Arr = o1.split(":");
var o2Arr = o2.split(":");
var o1Int = new Array();
var o2Int = new Array();
for (var i = 0; i < 2; i++) {
o1Int[i] = parseInt(o1Arr[i]);
o2Int[i] = parseInt(o2Arr[i]);
}
// 此处不是正则表达式
var second1Arr = o1Arr[2].split(".");
var second2Arr = o2Arr[2].split(".");
o1Int[2] = parseInt(second1Arr[0]);
o2Int[2] = parseInt(second2Arr[0]);
if (second1Arr.length == 2) {
o1Int[3] = parseInt(second1Arr[1]);
} else {
o1Int[3] = 0;
}
if (second2Arr.length == 2) {
o2Int[3] = parseInt(second2Arr[1]);
} else {
o2Int[3] = 0;
}
for (var i = 0; i < o1Int.length; i++) {
if (o1Int[i] == o2Int[i]) {
continue;
}
return o1Int[i] - o2Int[i];
}
return 0;
};
timeArr.sort(comp);
for (var i = 0; i < timeArr.length; i++) {
console.log(timeArr[i]);
}
}
(over)