Skip to content

Commit e44b6d3

Browse files
committed
feat: add solutions to lc problem: No.0635
No.0635.Design Log Storage System
1 parent 073e223 commit e44b6d3

File tree

6 files changed

+491
-1
lines changed

6 files changed

+491
-1
lines changed

solution/0600-0699/0635.Design Log Storage System/README.md

+170
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,192 @@ logSystem.retrieve("2016:01:01:01:01:01", "2017:01:01:23:00:00", "Hour");
5959

6060
<!-- 这里可写通用的实现逻辑 -->
6161

62+
**方法一:字符串比较**
63+
64+
将日志的 `id``timestamp` 作为元组存入数组中,然后在 `retrieve()` 方法中,根据 `granularity` 截取 `start``end` 的相应部分,然后遍历数组,将符合条件的 `id` 加入结果数组中。
65+
66+
时间复杂度方面,`put()` 方法的时间复杂度为 $O(1)$,`retrieve()` 方法的时间复杂度为 $O(n)$,其中 $n$ 为数组的长度。
67+
6268
<!-- tabs:start -->
6369

6470
### **Python3**
6571

6672
<!-- 这里可写当前语言的特殊实现逻辑 -->
6773

6874
```python
75+
class LogSystem:
76+
def __init__(self):
77+
self.logs = []
78+
self.d = {
79+
"Year": 4,
80+
"Month": 7,
81+
"Day": 10,
82+
"Hour": 13,
83+
"Minute": 16,
84+
"Second": 19,
85+
}
86+
87+
def put(self, id: int, timestamp: str) -> None:
88+
self.logs.append((id, timestamp))
6989

90+
def retrieve(self, start: str, end: str, granularity: str) -> List[int]:
91+
i = self.d[granularity]
92+
return [id for id, ts in self.logs if start[:i] <= ts[:i] <= end[:i]]
93+
94+
95+
# Your LogSystem object will be instantiated and called as such:
96+
# obj = LogSystem()
97+
# obj.put(id,timestamp)
98+
# param_2 = obj.retrieve(start,end,granularity)
7099
```
71100

72101
### **Java**
73102

74103
<!-- 这里可写当前语言的特殊实现逻辑 -->
75104

76105
```java
106+
class LogSystem {
107+
private List<Log> logs = new ArrayList<>();
108+
private Map<String, Integer> d = new HashMap<>();
109+
110+
public LogSystem() {
111+
d.put("Year", 4);
112+
d.put("Month", 7);
113+
d.put("Day", 10);
114+
d.put("Hour", 13);
115+
d.put("Minute", 16);
116+
d.put("Second", 19);
117+
}
118+
119+
public void put(int id, String timestamp) {
120+
logs.add(new Log(id, timestamp));
121+
}
122+
123+
public List<Integer> retrieve(String start, String end, String granularity) {
124+
List<Integer> ans = new ArrayList<>();
125+
int i = d.get(granularity);
126+
String s = start.substring(0, i);
127+
String e = end.substring(0, i);
128+
for (var log : logs) {
129+
String t = log.ts.substring(0, i);
130+
if (s.compareTo(t) <= 0 && t.compareTo(e) <= 0) {
131+
ans.add(log.id);
132+
}
133+
}
134+
return ans;
135+
}
136+
}
137+
138+
class Log {
139+
int id;
140+
String ts;
141+
142+
Log(int id, String ts) {
143+
this.id = id;
144+
this.ts = ts;
145+
}
146+
}
147+
148+
/**
149+
* Your LogSystem object will be instantiated and called as such:
150+
* LogSystem obj = new LogSystem();
151+
* obj.put(id,timestamp);
152+
* List<Integer> param_2 = obj.retrieve(start,end,granularity);
153+
*/
154+
```
155+
156+
### **C++**
157+
158+
```cpp
159+
class LogSystem {
160+
public:
161+
LogSystem() {
162+
d["Year"] = 4;
163+
d["Month"] = 7;
164+
d["Day"] = 10;
165+
d["Hour"] = 13;
166+
d["Minute"] = 16;
167+
d["Second"] = 19;
168+
}
169+
170+
void put(int id, string timestamp) {
171+
logs.push_back({id, timestamp});
172+
}
173+
174+
vector<int> retrieve(string start, string end, string granularity) {
175+
vector<int> ans;
176+
int i = d[granularity];
177+
auto s = start.substr(0, i);
178+
auto e = end.substr(0, i);
179+
for (auto& [id, ts] : logs) {
180+
auto t = ts.substr(0, i);
181+
if (s <= t && t <= e) {
182+
ans.emplace_back(id);
183+
}
184+
}
185+
return ans;
186+
}
187+
188+
private:
189+
vector<pair<int, string>> logs;
190+
unordered_map<string, int> d;
191+
};
192+
193+
/**
194+
* Your LogSystem object will be instantiated and called as such:
195+
* LogSystem* obj = new LogSystem();
196+
* obj->put(id,timestamp);
197+
* vector<int> param_2 = obj->retrieve(start,end,granularity);
198+
*/
199+
```
200+
201+
### **Go**
202+
203+
```go
204+
type LogSystem struct {
205+
logs []pair
206+
d map[string]int
207+
}
208+
209+
func Constructor() LogSystem {
210+
d := map[string]int{
211+
"Year": 4,
212+
"Month": 7,
213+
"Day": 10,
214+
"Hour": 13,
215+
"Minute": 16,
216+
"Second": 19,
217+
}
218+
return LogSystem{[]pair{}, d}
219+
}
220+
221+
func (this *LogSystem) Put(id int, timestamp string) {
222+
this.logs = append(this.logs, pair{id, timestamp})
223+
}
224+
225+
func (this *LogSystem) Retrieve(start string, end string, granularity string) (ans []int) {
226+
i := this.d[granularity]
227+
s, e := start[:i], end[:i]
228+
for _, log := range this.logs {
229+
t := log.ts[:i]
230+
if s <= t && t <= e {
231+
ans = append(ans, log.id)
232+
}
233+
}
234+
return
235+
}
236+
237+
type pair struct {
238+
id int
239+
ts string
240+
}
77241
242+
/**
243+
* Your LogSystem object will be instantiated and called as such:
244+
* obj := Constructor();
245+
* obj.Put(id,timestamp);
246+
* param_2 := obj.Retrieve(start,end,granularity);
247+
*/
78248
```
79249

80250
### **...**

solution/0600-0699/0635.Design Log Storage System/README_EN.md

+165-1
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,177 @@ logSystem.retrieve(&quot;2016:01:01:01:01:01&quot;, &quot;2017:01:01:23:00:00&qu
5959
### **Python3**
6060

6161
```python
62-
62+
class LogSystem:
63+
def __init__(self):
64+
self.logs = []
65+
self.d = {
66+
"Year": 4,
67+
"Month": 7,
68+
"Day": 10,
69+
"Hour": 13,
70+
"Minute": 16,
71+
"Second": 19,
72+
}
73+
74+
def put(self, id: int, timestamp: str) -> None:
75+
self.logs.append((id, timestamp))
76+
77+
def retrieve(self, start: str, end: str, granularity: str) -> List[int]:
78+
i = self.d[granularity]
79+
return [id for id, ts in self.logs if start[:i] <= ts[:i] <= end[:i]]
80+
81+
82+
# Your LogSystem object will be instantiated and called as such:
83+
# obj = LogSystem()
84+
# obj.put(id,timestamp)
85+
# param_2 = obj.retrieve(start,end,granularity)
6386
```
6487

6588
### **Java**
6689

6790
```java
91+
class LogSystem {
92+
private List<Log> logs = new ArrayList<>();
93+
private Map<String, Integer> d = new HashMap<>();
94+
95+
public LogSystem() {
96+
d.put("Year", 4);
97+
d.put("Month", 7);
98+
d.put("Day", 10);
99+
d.put("Hour", 13);
100+
d.put("Minute", 16);
101+
d.put("Second", 19);
102+
}
103+
104+
public void put(int id, String timestamp) {
105+
logs.add(new Log(id, timestamp));
106+
}
107+
108+
public List<Integer> retrieve(String start, String end, String granularity) {
109+
List<Integer> ans = new ArrayList<>();
110+
int i = d.get(granularity);
111+
String s = start.substring(0, i);
112+
String e = end.substring(0, i);
113+
for (var log : logs) {
114+
String t = log.ts.substring(0, i);
115+
if (s.compareTo(t) <= 0 && t.compareTo(e) <= 0) {
116+
ans.add(log.id);
117+
}
118+
}
119+
return ans;
120+
}
121+
}
122+
123+
class Log {
124+
int id;
125+
String ts;
126+
127+
Log(int id, String ts) {
128+
this.id = id;
129+
this.ts = ts;
130+
}
131+
}
132+
133+
/**
134+
* Your LogSystem object will be instantiated and called as such:
135+
* LogSystem obj = new LogSystem();
136+
* obj.put(id,timestamp);
137+
* List<Integer> param_2 = obj.retrieve(start,end,granularity);
138+
*/
139+
```
140+
141+
### **C++**
142+
143+
```cpp
144+
class LogSystem {
145+
public:
146+
LogSystem() {
147+
d["Year"] = 4;
148+
d["Month"] = 7;
149+
d["Day"] = 10;
150+
d["Hour"] = 13;
151+
d["Minute"] = 16;
152+
d["Second"] = 19;
153+
}
154+
155+
void put(int id, string timestamp) {
156+
logs.push_back({id, timestamp});
157+
}
158+
159+
vector<int> retrieve(string start, string end, string granularity) {
160+
vector<int> ans;
161+
int i = d[granularity];
162+
auto s = start.substr(0, i);
163+
auto e = end.substr(0, i);
164+
for (auto& [id, ts] : logs) {
165+
auto t = ts.substr(0, i);
166+
if (s <= t && t <= e) {
167+
ans.emplace_back(id);
168+
}
169+
}
170+
return ans;
171+
}
172+
173+
private:
174+
vector<pair<int, string>> logs;
175+
unordered_map<string, int> d;
176+
};
177+
178+
/**
179+
* Your LogSystem object will be instantiated and called as such:
180+
* LogSystem* obj = new LogSystem();
181+
* obj->put(id,timestamp);
182+
* vector<int> param_2 = obj->retrieve(start,end,granularity);
183+
*/
184+
```
68185
186+
### **Go**
187+
188+
```go
189+
type LogSystem struct {
190+
logs []pair
191+
d map[string]int
192+
}
193+
194+
func Constructor() LogSystem {
195+
d := map[string]int{
196+
"Year": 4,
197+
"Month": 7,
198+
"Day": 10,
199+
"Hour": 13,
200+
"Minute": 16,
201+
"Second": 19,
202+
}
203+
return LogSystem{[]pair{}, d}
204+
}
205+
206+
func (this *LogSystem) Put(id int, timestamp string) {
207+
this.logs = append(this.logs, pair{id, timestamp})
208+
}
209+
210+
func (this *LogSystem) Retrieve(start string, end string, granularity string) (ans []int) {
211+
i := this.d[granularity]
212+
s, e := start[:i], end[:i]
213+
for _, log := range this.logs {
214+
t := log.ts[:i]
215+
if s <= t && t <= e {
216+
ans = append(ans, log.id)
217+
}
218+
}
219+
return
220+
}
221+
222+
type pair struct {
223+
id int
224+
ts string
225+
}
226+
227+
/**
228+
* Your LogSystem object will be instantiated and called as such:
229+
* obj := Constructor();
230+
* obj.Put(id,timestamp);
231+
* param_2 := obj.Retrieve(start,end,granularity);
232+
*/
69233
```
70234

71235
### **...**

0 commit comments

Comments
 (0)