题目地址:https://leetcode-cn.com/problems/design-hit-counter/

题目描述

Design a hit counter which counts the number of hits received in the past 5 minutes.

Each function accepts a timestamp parameter (in seconds granularity) and you may assume that calls are being made to the system in chronological order (ie, the timestamp is monotonically increasing). You may assume that the earliest timestamp starts at 1.

It is possible that several hits arrive roughly at the same time.

Example:

HitCounter counter = new HitCounter();

// hit at timestamp 1.
counter.hit(1); // hit at timestamp 2.
counter.hit(2); // hit at timestamp 3.
counter.hit(3); // get hits at timestamp 4, should return 3.
counter.getHits(4); // hit at timestamp 300.
counter.hit(300); // get hits at timestamp 300, should return 4.
counter.getHits(300); // get hits at timestamp 301, should return 3.
counter.getHits(301);

Follow up:
What if the number of hits per second could be very large? Does your design scale?

题目大意

设计一个敲击计数器,使它可以统计在过去5分钟内被敲击次数。

每个函数会接收一个时间戳参数(以秒为单位),你可以假设最早的时间戳从1开始,且都是按照时间顺序对系统进行调用(即时间戳是单调递增)。

在同一时刻有可能会有多次敲击。

解题方法

字典

我们需要一个数据结构大小为300,能保存时间和该时间下对应的敲击次数。我选择的是字典。

当字典的元素个数超过了300时,应该把时间最早的删除掉,C++中map使用红黑树实现的,所以begin()就是最小的元素,将其删除即可。

查找300秒内的敲击次数时,需要遍历一遍map,如果时间窗在300秒以内,累加次数。

C++代码如下:

class HitCounter {
public:
/** Initialize your data structure here. */
HitCounter() {
} /** Record a hit.
@param timestamp - The current timestamp (in seconds granularity). */
void hit(int timestamp) {
if (m_.size() >= 300) {
m_.erase(m_.begin());
}
m_[timestamp] ++;
} /** Return the number of hits in the past 5 minutes.
@param timestamp - The current timestamp (in seconds granularity). */
int getHits(int timestamp) {
int count = 0;
for (auto& h : m_) {
if (h.first > timestamp - 300)
count += h.second;
}
return count;
}
private:
map<int, int> m_;
}; /**
* Your HitCounter object will be instantiated and called as such:
* HitCounter* obj = new HitCounter();
* obj->hit(timestamp);
* int param_2 = obj->getHits(timestamp);
*/

日期

2019 年 9 月 24 日 —— 梦见回到了小学,小学已经芳草萋萋破败不堪

【LeetCode】362. Design Hit Counter 解题报告 (C++)的更多相关文章

  1. LeetCode 362. Design Hit Counter

    原题链接在这里:https://leetcode.com/problems/design-hit-counter/description/ 题目: Design a hit counter which ...

  2. [LeetCode] 362. Design Hit Counter 设计点击计数器

    Design a hit counter which counts the number of hits received in the past 5 minutes. Each function a ...

  3. [LC] 362. Design Hit Counter

    Design a hit counter which counts the number of hits received in the past 5 minutes. Each function a ...

  4. 362. Design Hit Counter

    这个傻逼题..我没弄明白 you may assume that calls are being made to the system in chronological order (ie, the ...

  5. 【LeetCode】456. 132 Pattern 解题报告(Python)

    [LeetCode]456. 132 Pattern 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

  6. 【LeetCode】554. Brick Wall 解题报告(Python)

    [LeetCode]554. Brick Wall 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

  7. LeetCode 2 Add Two Sum 解题报告

    LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...

  8. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  9. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

随机推荐

  1. flask分页功能:基于flask-sqlalchemy和jinja2

    先看源码: @app.route('/movie', methods=['GET', 'POST']) @app.route('/home', methods=['GET', 'POST']) @ap ...

  2. 【MarkDown】--使用教程

    MarkDown使用教程 目录 MarkDown使用教程 一. 常用设置 1.1 目录 1.2 标题 1.3 文本样式 (1)引用 (2)高亮 (3)强调 (4)水平线 (5)上下标 (6)插入代码 ...

  3. 位运算符在JS中的妙用

    正文 位运算 JavaScript 中最臭名昭著的 Bug 就是 0.1 + 0.2 !== 0.3,因为精度的问题,导致所有的浮点运算都是不安全的,具体原因可详见<0.1 + 0.2不等于0. ...

  4. minSdkVersion、targetSdkVersion、targetApiLevel的区别

    在AndroidMenifest.xml中,常常会有下面的语句:  <uses-sdk android:minSdkVersion="4" android:targetSdk ...

  5. oracle中注释都是问号?中文显示不出来问题

    本人在工作中需要把开发上的库恢复到自己的虚拟机里面,然而捣鼓了许久建立好数据库之后,在使用建表语句初始化表的时候,发现注释都是????? 然后一脸懵逼不知何解,网上一大堆是说修改环境变量 NLS_LA ...

  6. 注解开发中的@Results注解使用

    package com.hope.dao;import com.hope.domain.User;import com.sun.xml.internal.bind.v2.model.core.ID;i ...

  7. 将前端请求中的数据绑定到Spring MVC响应方法中参数的四种方法

    一.映射URL绑定的占位符到方法参数 1.方法 使用@PathVariable注解 2.代码示例 a.接收请求方法 @RequestMapping(value = "/deleteInfo/ ...

  8. 【C/C++】string的长度

    一般用 s.length() s.size() 两种 size也可以用于vector string和vector的区别 string输入直接cin vector一般类似压栈pushback 输入一般是 ...

  9. .gitignore文件作用

    目录 一.简介 二.常用规则 三.详细 一.简介 一般来说每个Git项目中都需要一个.gitignore文件,这个文件的作用就是告诉Git哪些文件不需要添加到版本管理中. 意思就是本地修改完项目后,上 ...

  10. vue文件上传及压缩(canvas实现压缩)

    // 读取文件结果 afterRead(files) { let that = this; let file = files.file; if (file === undefined) { retur ...