其他分享
首页 > 其他分享> > leetcode649. Dota2 参议院

leetcode649. Dota2 参议院

作者:互联网

解题思路

利用两个队列来模拟循环队列

代码

class Solution {
    public String predictPartyVictory(String s) {
        Deque<Integer> r = new ArrayDeque<>();
        Deque<Integer> d = new ArrayDeque<>();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == 'R') {
                r.addLast(i);
            } else {
                d.addLast(i);
            }
        }
        while (!r.isEmpty() && !d.isEmpty()) {
            if (r.peekFirst() < d.peekFirst()) {
                r.addLast(r.peekFirst() + s.length());
            } else {
                d.addLast(d.peekFirst() + s.length());
            }
            r.removeFirst();
            d.removeFirst();
        }
        return r.isEmpty() ? "Dire" : "Radiant";
    }
}

标签:peekFirst,Deque,String,addLast,length,isEmpty,Dota2,参议院,leetcode649
来源: https://www.cnblogs.com/void-white/p/14119236.html