12/11
用队列模拟
class Solution {
public String predictPartyVictory(String senate) {
int n = senate.length();
Queue<Integer> R = new LinkedList<>();
Queue<Integer> D = new LinkedList<>();
for (int i = 0; i < n; i++) {
if (senate.charAt(i) == 'R') {
R.offer(i);
} else {
D.offer(i);
}
}
while (!R.isEmpty()&&!D.isEmpty()) {
int Rfront = R.poll();
int Dfront = D.poll();
if (Rfront < Dfront) {
R.offer(Rfront + n);
} else {
D.offer(Dfront + n);
}
}
return R.isEmpty()?"Dire":"Radiant";
}
}

本文介绍了一个使用队列数据结构模拟Radiant与Dire两方博弈的过程。通过不断比较双方的当前位置,胜者将继续留在比赛中,并在下一轮重新加入队列尾部。此过程一直持续到一方完全被淘汰出局。
437

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



