[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,这个系统主要目的就是降低存储成本,在容忍磁盘,主机,机架,数据中心的同时提 ...
随机推荐
- django-单表操作
#######单表操作######## 前面视图层,模板层.路由层都写了大概,项目肯定是会和数据库打交道,那就讲讲orm的单表查询吧,直接写过一点点,不太全面. 1.项目刚创建好,我们需要在setti ...
- [vue warn]:typeError:_this.getMounted.forEach is not a function
问题:报错 解决:forEach前面给数组,自己放的是Json,所以报错
- 【模板】素数测试(Miller-Rabin测试)
基础素数测试模板 对于大数的素性判断,目前Miller-Rabin算法应用最广泛.一般底数仍然是随机选取,但当待测数不太大时,选择测试底数就有一些技巧了.比如,如果 被测数小于4759123141,那 ...
- poj_2249_Binomial Showdown
In how many ways can you choose k elements out of n elements, not taking order into account? Write a ...
- ABAP术语-Function Group
Function Group 原文:http://www.cnblogs.com/qiangsheng/archive/2008/02/13/1067699.html Group of logical ...
- java递归 斐波那契数列递归与非递归实现
递归简单来说就是自己调用自己, 递归构造包括两个部分: 1.定义递归头:什么时候需要调用自身方法,如果没有头,将陷入死循环 2.递归体:调用自身方法干什么 递归是自己调用自己的方法,用条件来判断调用什 ...
- java后台去除JSON数组的重复值
假设原始Json数组是这样的 原始JSONArry:[{"Value":"15153129877","Key":"09770985 ...
- Python学习之property
Python中使用Property函数可以将类中的函数当作属性来调用. 案例 __metaclass__=type class Rectangle: def __init__(self): self. ...
- python入门——Anaconda安装
初学Python,可以选择python原始的IDE,但原始的IDE在使用过程中需要自己安装各种包,个人觉得初学者不需要将时间花在这些上面,而是应该直接学习python程序,这些比较杂的事情可以在以后的 ...
- stm32+lwip(三):TCP测试
我是卓波,很高兴你来看我的博客. 系列文章: stm32+lwip(一):使用STM32CubeMX生成项目 stm32+lwip(二):UDP测试 stm32+lwip(三):TCP测试 stm32 ...