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。

最终代码

<?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);
 */ 

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

转载于:https://my.oschina.net/u/2246923/blog/3044924

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值