[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,这个系统主要目的就是降低存储成本,在容忍磁盘,主机,机架,数据中心的同时提 ...
随机推荐
- AngularJS 三 控制器和事件
AngularJS控制器: ngularJS中的控制器是一个使用 $ scope 对象维护应用程序数据和行为的JavaScript函数. 您可以将属性和方法附加到控制器函数内的 $ scope 对象 ...
- android(eclipse)广播机制知识梳理(三)
1:分类: 标准广播:没有先后顺序,无法被截断 有序广播:又先后顺序,可以截断 2:接收广播:首先进行注册,注册的方式有静态注册和动态注册.也就是在代码中注册和在AndroidManifest ...
- Django-rest-framework(四)router
在上一节viewsets中,我们提到了route的方式使用,在这一节,我们将仔细介绍drf 的router机制.简单来说,router的作用就是将viewset对象的url映射关系提取出来. 简单使用 ...
- [UNIX]UNIX常用命令总结
(1)查看服务器IP信息 $netstat -in (2)查看挂载磁盘信息 #sam #需要在root账号下查看
- vue.js中的slot
vue.js 中的 slot 一.slot 的作用 调用组件的时候,对于数据,我们会用props将数据从父组件传至子组件.但是,如果从父组件到子组件,单纯是页面局部渲染的改变,slot会更合适. 二. ...
- ABAP术语-Document Number
Document Number 原文:http://www.cnblogs.com/qiangsheng/archive/2008/01/28/1055636.html Key which ident ...
- HTTP缓存初探
缓存的作用 用户访问一个web页面的频率远高于web页面更新的频率,因此多数时候用户从服务器获取的html.js.css以及图片等内容都是相同的,如果每次访问都从服务器获取这些静态内容即降低了页面加载 ...
- 关于linux命令的说明
开始前我们必须先认识绝对路径与相对路径 绝对路径是从盘符开始的路径 :例如:/etc/sysconfig/network (从根直接指到network) 相对路径是从当前自己所在位置开始的路径:例如我 ...
- 精干货! Java 后端程序员 1 年工作经验总结
一.引言 毕业已经一年有余,这一年里特别感谢技术管理人员的器重,以及同事的帮忙,学到了不少 东西.这一年里走过一些弯路,也碰到一些难题,也受到过做为一名开发却经常为系统维护 和发布当救火队员的苦恼 ...
- 【c学习-9】
/*内存练习*/ #include #include//引入内存分配库文件 int main(){ double* x; //定义整型指针和double指针 int* y; x=(double*)ma ...