[Swift]LeetCode981. 基於時間的鍵值存儲 | Time Based Key-Value Store

原文地址:http://www.javashuo.com/article/p-fbgskrlj-md.html html

Create a timebased key-value store class TimeMap, that supports two operations.app

1. set(string key, string value, int timestamp)函數

  • Stores the key and value, along with the given timestamp.

2. get(string key, int timestamp)測試

  • Returns a value such that set(key, value, timestamp_prev)was called previously, with timestamp_prev <= timestamp.
  • If there are multiple such values, it returns the one with the largest timestamp_prev.
  • If there are no values, it returns the empty string (""). 

Example 1:spa

Input: inputs = ["TimeMap","set","get","get","set","get","get"], inputs = [[],["foo","bar",1],["foo",1],["foo",3],["foo","bar2",4],["foo",4],["foo",5]] Output: [null,null,"bar","bar",null,"bar2","bar2"] Explanation:   TimeMap kv;   kv.set("foo", "bar", 1); // store the key "foo" and value "bar" along with timestamp = 1   kv.get("foo", 1); // output "bar"   kv.get("foo", 3); // output "bar" since there is no value corresponding to foo at timestamp 3 and timestamp 2, then the only value is at timestamp 1 ie "bar"   kv.set("foo", "bar2", 4);   kv.get("foo", 4); // output "bar2"   kv.get("foo", 5); //output "bar2"   

Example 2:code

Input: 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]] Output: [null,null,null,"","high","high","low","low"] 

Note:htm

  1. All key/value strings are lowercase.
  2. All key/value strings have length in the range [1, 100]
  3. The timestamps for all TimeMap.set operations are strictly increasing.
  4. 1 <= timestamp <= 10^7
  5. TimeMap.set and TimeMap.get functions will be called a total of 120000 times (combined) per test case.

建立一個基於時間的鍵值存儲類 TimeMap,它支持下面兩個操做:blog

1. set(string key, string value, int timestamp)ip

  • 存儲鍵 key、值 value,以及給定的時間戳 timestamp

2. 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. 全部的鍵/值字符串都是小寫的。
  2. 全部的鍵/值字符串長度都在 [1, 100] 範圍內。
  3. 全部 TimeMap.set 操做中的時間戳 timestamps 都是嚴格遞增的。
  4. 1 <= timestamp <= 10^7
  5. TimeMap.set 和 TimeMap.get 函數在每一個測試用例中將(組合)調用總計 120000 次。

 2180 ms

 1 class TimeMap {
 2     var d:[Data]
 3     var idx:[String:Int]
 4 
 5     /** Initialize your data structure here. */
 6     init() {
 7         d = [Data]() 
 8         idx = [String:Int]()
 9     }
10     
11     func set(_ key: String, _ value: String, _ timestamp: Int) {
12         var lt = gets(key, timestamp)
13         var dd:Data = Data(value, lt, timestamp)
14         idx[key] = d.count
15         d.append(dd)        
16     }
17     
18     func get(_ key: String, _ timestamp: Int) -> String {
19         var i:Int = gets(key, timestamp)
20         if i == -1
21         {
22             return String()
23         }
24         else
25         {
26             return d[i].value
27         }
28     }
29     
30     func gets(_ key: String, _ ts: Int) -> Int {
31         if idx[key] == nil
32         {
33             return -1
34         }
35         var p:Int = idx[key]!
36         while(p != -1 && d[p].ts > ts)
37         {
38             p = d[p].last
39         }
40         return p        
41     }
42 }
43 
44 struct Data
45 {
46     var value:String = String()
47     var last:Int = 0
48     var ts:Int = 0
49     
50     init()
51     {
52         
53     }
54     
55     init(_ value:String,_ last:Int,_ ts:Int)
56     {
57         self.value = value
58         self.last = last
59         self.ts = ts
60     }
61 }
62 
63 /**
64  * Your TimeMap object will be instantiated and called as such:
65  * let obj = TimeMap()
66  * obj.set(key, value, timestamp)
67  * let ret_2: String = obj.get(key, timestamp)
68  */
相關文章
相關標籤/搜索