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

发布时间:2019-08-08 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Leetcode PHP题解--D50 933. Number of Recent Calls脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

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。

最终代码

@H_512_29@
PE="button" class="copyCode code-tool" data-toggle="tooltip" data-placement="top" data-clipboard-text="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); */ " tITle="" data-original-title="复制">
<?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);
 */ 

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

脚本宝典总结

以上是脚本宝典为你收集整理的Leetcode PHP题解--D50 933. Number of Recent Calls全部内容,希望文章能够帮你解决Leetcode PHP题解--D50 933. Number of Recent Calls所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。