IT序号网

c++之面试算法查询

zlslch 2023年07月08日 编程语言 24 0

<分区>

Given constant integers x and t, write a function that takes no argument and returns true if the function has been called x number of times in last t secs.

这是我对一个可能算法的伪代码/C++ 实现,但我不确定它是否正确/有效:

const int x; 
const int t; 
vector<long> v; 
boolean countNumberOfTimesBeenCalled(){ 
    int numberOfCallsInLastTSeconds=0; 
    v.push_back(System.currentTimeInMillis()); 
    for(int x=0; x<v.size();x++){ 
        if((v.at(x)>=(System.currentTimeInMillis()-1000*t))&&(v.at(x)<=System.currentTimeInMillis()) 
            numberOfCallsInLastTSeconds++; 
    } 
    if(numberOfCallsInLastTSeconds==x) 
        return true; 
    else 
        return false; 
} 

谁能提出替代方案?


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!