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:

  1. There will be at most 300 operations of Put or Retrieve.
  2. Year ranges from [2000,2017]. Hour ranges from [00,23].
  3. 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]的更多相关文章

  1. [LeetCode] Design Log Storage System 设计日志存储系统

    You are given several logs that each log contains a unique id and timestamp. Timestamp is a string t ...

  2. LeetCode Design Log Storage System

    原题链接在这里:https://leetcode.com/problems/design-log-storage-system/description/ 题目: You are given sever ...

  3. Design Log Storage System

    You are given several logs that each log contains a unique id and timestamp. Timestamp is a string t ...

  4. [LeetCode] 642. Design Search Autocomplete System 设计搜索自动补全系统

    Design a search autocomplete system for a search engine. Users may input a sentence (at least one wo ...

  5. Bigtable: A Distributed Storage System for Structured Data

    https://static.googleusercontent.com/media/research.google.com/en//archive/bigtable-osdi06.pdf Abstr ...

  6. Storage System and File System Courses

    I researched a lot about storage system classes given at good universities this year. This had two r ...

  7. 1.1 Introduction中 Kafka as a Storage System官网剖析(博主推荐)

    不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Kafka as a Storage System kafka作为一个存储系统 An ...

  8. Blockstack: A Global Naming and Storage System Secured by Blockchains

    作者:Muneeb Ali, Jude Nelson, Ryan Shea, and Michael Freedman Blockstack Labs and Princeton University ...

  9. f4: Facebook’s Warm BLOB Storage System——Erasure Code

    Facebook在OSDI 2014上发表论文f4: Facebook's Warm BLOB Storage System,这个系统主要目的就是降低存储成本,在容忍磁盘,主机,机架,数据中心的同时提 ...

随机推荐

  1. mac install PyQt5

    1. install brew /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/insta ...

  2. js 事件委托 事件代理

    JavaScript高级程序设计上解释:事件委托就是利用事件冒泡,只指定一个事件处理程序,就可以管理某一类型的所有事件. 通过例子类比: 有三个同事预计会在周一收到快递.为签收快递,有两种办法:一是三 ...

  3. c#总结最近的几项重要代码

    java的代码就不说了,毕竟不是我的主业. 1.c#数据库连接池Hikari. (1)动态加载各类数据库驱动 (2)支持简单配置文件 (3)支持按照名称多数据库调用 (4)使用简洁 单数据库使用: H ...

  4. aix下oracle 12.1.0.2 asmca不能打开的故障

    因为要添加一个新的13T磁盘组,所以决定通过asmca处理. 结果输入asmca之后,没有反应,前后两天都是如此. 第三天,IBM的存储工程师已经把心的MPIO挂上,如果还无法操作,只能使用asmcm ...

  5. 你的sql查询为什么这么慢?

    做后台开发的程序猿通常需要写各种各样的sql,可很多时候写出来的sql虽然能满足功能性需求,性能上却不尽人意.如果业务复杂,表结构和索引设计又不合理的话,写出来的sql执行时间可能会达到几十甚至上百秒 ...

  6. 关于mysql8.0.11版本在win10安装

    新的mysql版本没有.exe文件一键安装,网上找了教程,自己搞了下 首先是在菜鸟教程 http://www.runoob.com/mysql/mysql-install.html 根据它的提示下载w ...

  7. vue渲染自定义json数据

    <template> <div class="wrap"> <div class="main"> <div class ...

  8. Mysql基础1-基础语法-字段类型

    主要: 基础 字段类型 基础 基本概念 1) 数据库分类 层次数据库,网状数据库,关系数据库 常见:SQL Server, Oracle,infomix,sybase,ibmDB2,Mysql 2)数 ...

  9. HDU暑假多校第三场H.Monster Hunter

    一.题意 给定一个树状地图,每个树节点上有一只怪物,打死一只怪物的过程中将会消耗A点HP,打死之后将会获得B点HP.因为树状结构,所以每只怪物必须先打死父节点的怪兽之后在打死子节点的怪物.现在,给定每 ...

  10. 总结Verilog中always语句的使用

    always语句包括的所有行为语句构成了一个always语句块.该always语句块从仿真0时刻开始执行其中的行为语句:最后一条执行完成后,再开始执行其中的第一条语句,如此往复循环,直到整个仿真结束. ...