[leetcode-635-Design Log Storage System]
You are given several logs that 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.
Design a log storage system to implement the following functions:
void Put(int id, string timestamp): Given a log's unique id and timestamp, store the log in your storage system.
int[] Retrieve(String start, String end, String granularity): Return the id of logs whose timestamps are within the range from start to end. Start and end all have the same format as timestamp. However, granularity means the time level for consideration. For example, start = "2017:01:01:23:59:59", end = "2017:01:02:23:59:59", granularity = "Day", it means that we need to find the logs within the range from Jan. 1st 2017 to Jan. 2nd 2017.
Example 1:
put(1, "2017:01:01:23:59:59");
put(2, "2017:01:01:22:59:59");
put(3, "2016:01:01:00:00:00");
retrieve("2016:01:01:01:01:01","2017:01:01:23:00:00","Year"); // return [1,2,3], because you need to return all logs within 2016 and 2017.
retrieve("2016:01:01:01:01:01","2017:01:01:23:00:00","Hour"); // return [1,2], because you need to return all logs start from 2016:01:01:01 to 2017:01:01:23, where log 3 is left outside the range.
Note:
- There will be at most 300 operations of Put or Retrieve.
- Year ranges from [2000,2017]. Hour ranges from [00,23].
- Output for Retrieve has no order required.
思路:
用map去存储时间戳和id之间的映射,
将字符串形式的时间戳处理成整数形式的数组,便于比较。
还有就是将起始置为0,结束置为最大99,这样就能保证比较容易找到在这区间内的了。
map<string, vector<int>>mp;
map<string, int>idmap;
map<string, int>mapping;
Solution()
{
mapping["Year"] = ;
mapping["Month"] = ;
mapping["Day"] = ;
mapping["Hour"] = ;
mapping["Minute"] = ;
mapping["Second"] = ;
}
vector<int> convert(string s)
{
for (int i = ; i < s.size();i++)if (s[i] == ':')s[i] = ' ';
stringstream ss(s);
int x;
vector<int>a;
while (ss >> x)a.push_back(x);
return a;
} void put(int id, string timestamp)
{
idmap[timestamp] = id;
mp[timestamp] = convert(timestamp);
}
vector<int> retrieve(string s, string e, string gra)
{
vector<int>result;
vector<int>from = convert(s), to = convert(e);
for (int i = mapping[gra] + ; i < ;i++)
{
from[i] = ;
to[i] = ;
}
for (auto &it : mp)
{
if (it.second >= from && it.second <= to)
{
result.push_back(idmap[it.first]);
}
}
return result;
}
[leetcode-635-Design Log Storage System]的更多相关文章
- [LeetCode] Design Log Storage System 设计日志存储系统
You are given several logs that each log contains a unique id and timestamp. Timestamp is a string t ...
- LeetCode Design Log Storage System
原题链接在这里:https://leetcode.com/problems/design-log-storage-system/description/ 题目: You are given sever ...
- Design Log Storage System
You are given several logs that each log contains a unique id and timestamp. Timestamp is a string t ...
- [LeetCode] 642. Design Search Autocomplete System 设计搜索自动补全系统
Design a search autocomplete system for a search engine. Users may input a sentence (at least one wo ...
- Bigtable: A Distributed Storage System for Structured Data
https://static.googleusercontent.com/media/research.google.com/en//archive/bigtable-osdi06.pdf Abstr ...
- Storage System and File System Courses
I researched a lot about storage system classes given at good universities this year. This had two r ...
- 1.1 Introduction中 Kafka as a Storage System官网剖析(博主推荐)
不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Kafka as a Storage System kafka作为一个存储系统 An ...
- Blockstack: A Global Naming and Storage System Secured by Blockchains
作者:Muneeb Ali, Jude Nelson, Ryan Shea, and Michael Freedman Blockstack Labs and Princeton University ...
- f4: Facebook’s Warm BLOB Storage System——Erasure Code
Facebook在OSDI 2014上发表论文f4: Facebook's Warm BLOB Storage System,这个系统主要目的就是降低存储成本,在容忍磁盘,主机,机架,数据中心的同时提 ...
随机推荐
- C# 通过socket实现UDP 通信
UDP不属于面向连接的通信,在选择使用协议的时候,选择UDP必须要谨慎.在网络质量令人十分不满意的环境下,UDP协议数据包丢失会比较严重.但是由于UDP的特性:它不属于连接型协议,因而具有资源消耗小, ...
- insertAdjacentHTML与innerHTML
insertAdjacentHTML:insertAdjacentHTML() 将指定的文本解析为HTML或XML,并将结果节点插入到DOM树中的指定位置.它不会重新解析它正在使用的元素,因此它不会破 ...
- webpack——安装报错及解决办法
①先删除 删除全局webpack-cli webpack4.x开始官方文档是说要安装cli所以如果的在用4.+就需要卸载cli npm uninstall -g webpack-cli # 注释给我这 ...
- 分享一个展示文章列表的CSS样式
最近在帮朋友处理一个网站前端显示文章列表的时候,其中有个变通的思路,现整理出来留给有需要的朋友参考及自己备忘. 显示效果为:标题左对齐,日期右对齐. 标题和日期中间用常规的原点(“.”) 代替,显示效 ...
- meclipse6.5破解
package com.test.ssh.common; import java.text.DecimalFormat; import java.text.NumberFormat; import ...
- vs2017中的scanf_s
在visual studio 2017中格式化输入函数不同于其他c/c++编译器使用scanf,而是使用scanf_s. scanf_s相比较于scanf来说更安全,因为使用scanf_s函数需要有一 ...
- chromium之tracked
//------------------------------------------------------------------------------ // Tracked is the b ...
- 实践笔记-VA05 销售订单清单 增加字段
现在都自开发很多报表 ,估计没有多少人 用 VA05 1.在结构 VBMTVZ 中增加需要的字段 2.表t180a 中 添加一条 “添加字段”的数据,如下: 3.取值 修改程序 INCLUDE V ...
- Lintcode算法
题目: 给出一组非负整数,重新排列他们的顺序把他们组成一个最大的整数. 样例 给出 [1, 20, 23, 4, 8],返回组合最大的整数应为8423201. 思路:直接交换两个数,然后判断交换之后的 ...
- PHP如何实现99乘法表?
看到这个问题,可能大家更多的是考虑到用for循环,个人觉得使用for循环太影响程序性能.推荐使用递归处理. /** * Title : 递归实现99乘法表 * Author : Bruceqi * ...