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. 触发器 :new和 :old的使用

    :new --为一个引用最新的列值;:old --为一个引用以前的列值; 这两个变量只有在使用了关键字 "FOR EACH ROW"时才存在,且update语句两个都有,而inse ...

  2. Navicat for Mysql中错误提示索引过长1071-max key length is 767 byte

    1.建用户信息表 tb_person_info create table tb_person_info( user_id int(10) auto_increment, `name` varchar( ...

  3. 上传文件到阿里云linux服务器

    在“运行”中输入cmd,打开控制台,切换到刚才Putty的安装目录下,我的是E:\Putty,然后输入pscp命令,我们需要这个命令来实现文件的上传.如下图所示,命令格式为: pscp D:\recy ...

  4. tp5多条件查询

    ->where('m.user_nickname|w.nickname|c.companyname','like','%'.$search.'%')\

  5. python学习之字符串常用方法和格式化字符串

    Python中的字符串同样适用标准的序列操作(索引,分片,乘法,成员判断,求长度,取最小值和最大值),但因为字符串是不可变的,因此字符串不支持分片赋值. s='http://www.baidu.com ...

  6. docker和docker compose常用操作命令

    首先区分一下docker中几个概念 Image:镜像,相当于一个root文件系统,不包含任何动态数据 Container:容器,镜像运行时的实体,实质是进程,容器进程运行于属于自己的独立的命名空间 d ...

  7. crest value &minimum

    public class paixu { public static void main(String[] args) { double temp; double num[]={5.1, 7.12, ...

  8. 洛谷P4016 负载平衡问题

    题目描述 G 公司有 n 个沿铁路运输线环形排列的仓库,每个仓库存储的货物数量不等.如何用最少搬运量可以使 n 个仓库的库存数量相同.搬运货物时,只能在相邻的仓库之间搬运. 输入输出格式 输入格式: ...

  9. SAP ABAP Development Tools in Eclipseのセットアップ

    手順 1. Eclipse IDE インストール 以下からダウンロード.https://tools.hana.ondemand.com/#abap※2018/1月現在 Oxygen(4.7)詳細は割愛 ...

  10. PHP.42-TP框架商城应用实例-后台17-商品属性3-商品分类的修改与删除

    商品分类的修改 1.改表单Goods/edit.html,加下拉框 2.因为商品属性修改涉及商品属性表goods_attr{id,attr_value,attr_id,goods_id}与属性表att ...