import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 求出最终获胜帮派的名称 * @param s string字符串 * @return string字符串 */ public String predictVictory (String s) { // write code here Queue<Integer> rQueue=new ArrayDeque<>(); Queue<Integer> dQueue=new ArrayDeque<>(); for (int i = 0; i < s.length(); i++) { if(s.charAt(i)=='D') { dQueue.add(i); }else { rQueue.add(i); } } while(!rQueue.isEmpty()&&!dQueue.isEmpty()) { int rindex=rQueue.poll(); int dindex=dQueue.poll(); if(rindex<dindex) { rQueue.add(rindex+s.length()); }else { dQueue.add(dindex+s.length()); } } return rQueue.isEmpty()?"Dark":"Red"; } }
这一题我学的题解的思路,使用两个队列来记录红帮和黑帮人员的索引,然后进行比对,如果红帮索引靠前,那么黑帮的那个人员就会消失,反之,红方会消失。而不消失的会进入到队尾,我们可以直接给它的索引加n,这样可以体现出先后关系