You are given several logs, where each log contains a unique ID and timestamp. Timestamp is a string that has the following format: Year:Month:Day:Hour:Minute:Second
, for example, 2017:01:01:23:59:59
. All domains are zero-padded decimal numbers.
Implement the LogSystem
class:
LogSystem()
Initializes theLogSystem
object.void put(int id, string timestamp)
Stores the given log(id, timestamp)
in your storage system.int[] retrieve(string start, string end, string granularity)
Returns the IDs of the logs whose timestamps are within the range fromstart
toend
inclusive.start
andend
all have the same format astimestamp
, andgranularity
means how precise the range should be (i.e. to the exactDay
,Minute
, etc.). For example,start = "2017:01:01:23:59:59"
,end = "2017:01:02:23:59:59"
, andgranularity = "Day"
means that we need to find the logs within the inclusive range from Jan. 1st 2017 to Jan. 2nd 2017, and theHour
,Minute
, andSecond
for each log entry can be ignored.
Example 1:
Input ["LogSystem", "put", "put", "put", "retrieve", "retrieve"] [[], [1, "2017:01:01:23:59:59"], [2, "2017:01:01:22:59:59"], [3, "2016:01:01:00:00:00"], ["2016:01:01:01:01:01", "2017:01:01:23:00:00", "Year"], ["2016:01:01:01:01:01", "2017:01:01:23:00:00", "Hour"]] Output [null, null, null, null, [3, 2, 1], [2, 1]] Explanation LogSystem logSystem = new LogSystem(); logSystem.put(1, "2017:01:01:23:59:59"); logSystem.put(2, "2017:01:01:22:59:59"); logSystem.put(3, "2016:01:01:00:00:00"); // return [3,2,1], because you need to return all logs between 2016 and 2017. logSystem.retrieve("2016:01:01:01:01:01", "2017:01:01:23:00:00", "Year"); // return [2,1], because you need to return all logs between Jan. 1, 2016 01:XX:XX and Jan. 1, 2017 23:XX:XX. // Log 3 is not returned because Jan. 1, 2016 00:00:00 comes before the start of the range. logSystem.retrieve("2016:01:01:01:01:01", "2017:01:01:23:00:00", "Hour");
思路:核心思想就是 time -> key, value -> list of id; 用treemap自动排序time,然后取submap,submap是通过start granularity前面的 + 后面标准的,来取的,我觉得这个题的考点就是在于怎么取submap;
class LogSystem {
private String min, max;
private TreeMap<String, LinkedList<Integer>> treemap;
private HashMap<String, Integer> indexMap;
public LogSystem() {
// Year:Month:Day:Hour:Minute:Second
this.min = "2000:01:01:00:00:00";
this.max = "2017:12:31:23:59:59";
this.indexMap = new HashMap<>();
indexMap.put("Year", 4);
indexMap.put("Month", 7);
indexMap.put("Day", 10);
indexMap.put("Hour", 13);
indexMap.put("Minute", 16);
indexMap.put("Second", 19);
this.treemap = new TreeMap<>();
}
public void put(int id, String timestamp) {
treemap.putIfAbsent(timestamp, new LinkedList<>());
treemap.get(timestamp).add(id);
}
public List<Integer> retrieve(String start, String end, String granularity) {
int index = indexMap.get(granularity);
// 建成新的start和end;
String s = start.substring(0, index) + min.substring(index);
String e = end.substring(0, index) + max.substring(index);
Map<String, LinkedList<Integer>> targetMap = treemap.subMap(s, true, e, true);
List<Integer> res = new LinkedList<Integer>();
for(String key: targetMap.keySet()) {
res.addAll(targetMap.get(key));
}
return res;
}
}
/**
* Your LogSystem object will be instantiated and called as such:
* LogSystem obj = new LogSystem();
* obj.put(id,timestamp);
* List<Integer> param_2 = obj.retrieve(start,end,granularity);
*/