Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0e3ea43

Browse files
authored
Merge pull requestneetcode-gh#3596 from drxlx/0981-time-based-key-value-store
Create 0981-time-based-key-value-store.swift
2 parents44b0e00 +28a8486 commit0e3ea43

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
classTimeMap{
3+
varstore=[String:[(String, Int)]]()
4+
init(){
5+
6+
}
7+
8+
func set(_ key:String, _ value:String, _ timestamp:Int){
9+
store[key, default:[]].append((value, timestamp))
10+
}
11+
12+
func get(_ key:String, _ timestamp:Int)->String{
13+
varres=""
14+
varvalues=store[key]??[]
15+
16+
varl=0
17+
varr= values.count-1
18+
while l<= r{
19+
letm= l+((r- l)/2)
20+
ifvalues[m].1<= timestamp{
21+
res=values[m].0
22+
l= m+1
23+
}else{
24+
r= m-1
25+
}
26+
}
27+
28+
return res
29+
}
30+
}
31+
32+
/**
33+
* Your TimeMap object will be instantiated and called as such:
34+
* let obj = TimeMap()
35+
* obj.set(key, value, timestamp)
36+
* let ret_2: String = obj.get(key, timestamp)
37+
*/

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp