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

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

内容简介:这个题目说实在的,看得我一脸蒙蔽。返回自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);
 */

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


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

查看所有标签

猜你喜欢:

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

Programming Computer Vision with Python

Programming Computer Vision with Python

Jan Erik Solem / O'Reilly Media / 2012-6-22 / USD 39.99

If you want a basic understanding of computer vision's underlying theory and algorithms, this hands-on introduction is the ideal place to start. As a student, researcher, hacker, or enthusiast, you'll......一起来看看 《Programming Computer Vision with Python》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

MD5 加密
MD5 加密

MD5 加密工具