有点循环队列的意思(看题解写的)

class Solution {
public:
string predictPartyVictory(string senate) {
int l=senate.length();
queue<int>R;
queue<int>D;
int i=0;
while(i<l)
{
if(senate[i]=='R')
R.push(i);
if(senate[i]=='D')
D.push(i);
i++;
}
while(!R.empty()&&!D.empty())
{
int r=R.front();
int d=D.front();
if(R.front()<D.front())
{
D.pop();
R.pop();
R.push(r+l);
}
else
{
R.pop();
D.pop();
D.push(d+l);
}
}
return R.empty()?"Dire":"Radiant";
}
};
本文介绍了一个使用C++实现的两党博弈问题。通过构建循环队列来模拟参议员之间的对决过程,最终确定获胜方。该算法利用了队列的数据结构特性,实现了高效地模拟和判断。
990

被折叠的 条评论
为什么被折叠?



