gpt4 book ai didi

c++ - 面试算法查询

转载 作者:搜寻专家 更新时间:2023-10-31 01:51:45 26 4
gpt4 key购买 nike

<分区>

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;
}

谁能提出替代方案?

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com