- 基于时间的键值存储 难度[中等]
创建一个基于时间的键值存储类 TimeMap,它支持下面两个操作:
-
set(string key, string value, int timestamp)
- 存储键 key、值 value,以及给定的时间戳 timestamp。
-
get(string key, int timestamp)
- 返回先前调用 set(key, value, timestamp_prev) 所存储的值,其中 timestamp_prev <= timestamp。
- 如果有多个这样的值,则返回对应最大的 timestamp_prev 的那个值。
- 如果没有值,则返回空字符串("")。
示例 1:
输入:inputs = ["TimeMap","set","get","get","set","get","get"], inputs = [[],["foo","bar",1],["foo",1],["foo",3],["foo","bar2",4],["foo",4],["foo",5]]
输出:[null,null,"bar","bar",null,"bar2","bar2"]
解释:
TimeMap kv;
kv.set("foo", "bar", 1); // 存储键 "foo" 和值 "bar" 以及时间戳 timestamp = 1
kv.get("foo", 1); // 输出 "bar"
kv.get("foo", 3); // 输出 "bar" 因为在时间戳 3 和时间戳 2 处没有对应 "foo" 的值,所以唯一的值位于时间戳 1 处(即 "bar")
kv.set("foo", "bar2", 4);
kv.get("foo", 4); // 输出 "bar2"
kv.get("foo", 5); // 输出 "bar2"
示例 2:
输入:inputs = ["TimeMap","set","set","get","get","get","get","get"], inputs = [[],["love","high",10],["love","low",20],["love",5],["love",10],["love",15],["love",20],["love",25]]
输出:[null,null,null,"","high","high","low","low"]
提示:
- 所有的键/值字符串都是小写的。
- 所有的键/值字符串长度都在 [1, 100] 范围内。
- 所有 TimeMap.set 操作中的时间戳 timestamps 都是严格递增的。
- 1 <= timestamp <= 10^7
- TimeMap.set 和 TimeMap.get 函数在每个测试用例中将(组合)调用总计 120000 次。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/time-based-key-value-store
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解法一:嵌套HashMap
class TimeMap {
Map<String, Map<String,Integer>> map =new HashMap<>();
/** Initialize your data structure here. */
public TimeMap() {
this.map=new HashMap<>();
}
public void set(String key, String value, int timestamp) {
Map<String,Integer> values;
if(map.containsKey(key)){
values = map.get(key);
}else {
values = new HashMap<>();
}
values.put(value,timestamp);
map.put(key,values);
}
public String get(String key, int timestamp) {
Map<String,Integer> values = map.get(key);
int times=0;
if(values==null) return "";
String k ="";
for(String s :values.keySet()){
int t = values.get(s);
if(t<=timestamp && t>times) {
times=t;
k=s;
}
}
return k;
}
}
/**
* Your TimeMap object will be instantiated and called as such:
* TimeMap obj = new TimeMap();
* obj.set(key,value,timestamp);
* String param_2 = obj.get(key,timestamp);
*/
解法二:二分优化
class TimeMap {
class Node {
String v;
int t;
Node (String _v, int _t) {
v = _v; t = _t;
}
}
Map<String, List<Node>> map =new HashMap<>();
/** Initialize your data structure here. */
public TimeMap() {
this.map=new HashMap<>();
}
public void set(String key, String value, int timestamp) {
List<Node> values;
if(map.containsKey(key)){
values = map.get(key);
}else {
values = new ArrayList<>();
}
Node node = new Node(value,timestamp);
values.add(node);
map.put(key,values);
}
public String get(String key, int timestamp) {
List<Node> values = map.get(key);
if(values==null) return "";
int n =values.size();
int l=0,r=n-1;
while (l<r){
int mid = l+r+1>>1;
if(values.get(mid).t<=timestamp){
l=mid;
}else{
r=mid-1;
}
}
return values.get(r).t<=timestamp ? values.get(r).v:"";
}
}
/**
* Your TimeMap object will be instantiated and called as such:
* TimeMap obj = new TimeMap();
* obj.set(key,value,timestamp);
* String param_2 = obj.get(key,timestamp);
*/
此文章创于本人学习时的记录,如有错误或更优解还请指出