1. 我的代码
我的代码,长得像匹马,主要是比较时间那一块写得太冗长:
public static class LogSystem {
// List<String> stamps = new ArrayList<>();
HashMap<String[], Integer> map = new HashMap<>();
public LogSystem() {
}
public void put(int id, String timestamp) {
map.put(timestamp.split(":"), id);
}
public List<Integer> retrieve(String s, String e, String gra) {
int graIndex;
switch (gra) {
case "Year":
graIndex = 0;
break;
case "Month":
graIndex = 1;
break;
case "Day":
graIndex = 2;
break;
case "Hour":
graIndex = 3;
break;
case "Minute":
graIndex = 4;
break;
case "Second":
graIndex = 5;
break;
default:
graIndex = 0;
}
//01这种能parseInt吗,可以
String[] sArr = s.split(":");
String[] eArr = e.split(":");
ArrayList<Integer> res = new ArrayList<>();
boolean sTrue = false;
boolean eTrue = false;
for (String[] key : map.keySet()) {
for (int i = 0; i <= graIndex; i++) {
if (Integer.parseInt(key[i]) < Integer.parseInt(sArr[i])) {
break;
}
if (i < graIndex && Integer.parseInt(key[i]) > Integer.parseInt(sArr[i])) {
sTrue = true;
break;
}
if (i == graIndex && Integer.parseInt(key[i]) >= Integer.parseInt(sArr[i])) {
sTrue = true;
}
}
if (!sTrue){
continue;
}
for (int i = 0; i <= graIndex; i++) {
if (Integer.parseInt(key[i]) > Integer.parseInt(eArr[i])) {
break;
}
if (i < graIndex && Integer.parseInt(key[i]) < Integer.parseInt(eArr[i])) {
eTrue = true;
break;
}
if (i == graIndex && Integer.parseInt(key[i]) <= Integer.parseInt(eArr[i])) {
eTrue = true;
}
}
if (eTrue) {
res.add(map.get(key));
}
sTrue = false;
eTrue = false;
}
return res;
}
}
2. 别人简洁的代码
首先我摘录一段String的compareTo
方法的comment:
Compares two strings lexicographically.
The comparison is based on the Unicode value of each character in the strings.
所以不需要像我上面那样,把每个unit分开比较,而只需要比较lexicographically(字典序)就能找出target。
public static class LogSystem {
List<String[]> timestamps = new LinkedList<>();
List<String> units = Arrays.asList("Year", "Month", "Day", "Hour", "Minute", "Second");
int[] indices = new int[]{4,7,10,13,16,19};
public void put(int id, String timestamp) {
timestamps.add(new String[]{Integer.toString(id), timestamp});
}
public List<Integer> retrieve(String s, String e, String gra) {
List<Integer> res = new LinkedList<>();
int idx = indices[units.indexOf(gra)];
for (String[] timestamp : timestamps) {
if (timestamp[1].substring(0, idx).compareTo(s.substring(0, idx)) >= 0 &&
timestamp[1].substring(0, idx).compareTo(e.substring(0, idx)) <= 0) {
res.add(Integer.parseInt(timestamp[0]));
}
}
return res;
}
}