Leetcode PHP题解--D50 933. Number of Recent Calls

栏目: PHP · 发布时间: 6年前

内容简介:这个题目说实在的,看得我一脸蒙蔽。返回自3000毫秒到现在为止ping的次数(包括当前ping)。

D50 933. Number of Recent Calls

题目链接

933. Number of Recent Calls

题目分析

这个题目说实在的,看得我一脸蒙蔽。

返回自3000毫秒到现在为止ping的次数(包括当前ping)。

ping 函数时,传入的参数 t 为当前ping的毫秒数。

思路

其实是说,返回前3000毫秒内ping的次数。

把每次ping的毫秒数存起来,然后往回找3000毫秒内的ping。

即,给当前ping次数加1,直到当前毫秒数减前面ping的毫秒数大于3000。

最终代码

<?php
class RecentCounter {
    public $pings = [];
    public $head = 0;

    function __construct() {
        
    }
    function ping($t) {
        if(is_null($t)){
            return null;
        }
        $this->pings[] = $t;
        
        while(($this->pings[count($this->pings)-1]-$this->pings[$this->head])>3000){
            $this->head++;
        }
        return count($this->pings)-$this->head;
    }
}
/**
 * Your RecentCounter object will be instantiated and called as such:
 * $obj = RecentCounter();
 * $ret_1 = $obj->ping($t);
 */

若觉得本文章对你有用,欢迎用 爱发电 资助。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Advanced Web Metrics with Google Analytics, 2nd Edition

Advanced Web Metrics with Google Analytics, 2nd Edition

Brian Clifton / Sybex / 2010-3-15 / USD 39.99

Valuable tips and tricks for using the latest version of Google Analytics Packed with insider tips and tricks, this how-to guide is fully revised to cover the latest version of Google Analytics and sh......一起来看看 《Advanced Web Metrics with Google Analytics, 2nd Edition》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

SHA 加密
SHA 加密

SHA 加密工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具