题目:
我的解法:
class Solution {
public String predictPartyVictory(String senate) {
char[] array = senate.toCharArray();
int R_num = 0;
int D_num = 0;
List<Character> list = new ArrayList<Character>();
for(char c: array){
if(c=='R'){
R_num++;
}else{
D_num++;
}
list.add(c);
}
int index = 0;
while(R_num!=0 && D_num!=0){
if(list.get(index)!='0'){
for(int i=index+1; i<list.size(); i++){
if(list.get(i)!=list.get(index) && list.get(i)!='0'){
if(list.get(i)=='R'){
R_num--;
}else{
D_num--;
}
list.set(i, '0') ;
list.add(list.get(index));
index++;
break;
}
}
}else{
index++;
}
}
if(R_num==0){
return "Dire";
}else{
return "Radiant";
}
}
}
官方题解:
class Solution {
public String predictPartyVictory(String senate) {
int n = senate.length();
Queue<Integer> radiant = new LinkedList<Integer>();
Queue<Integer> dire = new LinkedList<Integer>();
for (int i = 0; i < n; ++i) {
if (senate.charAt(i) == 'R') {
radiant.offer(i);
} else {
dire.offer(i);
}
}
while (!radiant.isEmpty() && !dire.isEmpty()) {
int radiantIndex = radiant.poll(), direIndex = dire.poll();
if (radiantIndex < direIndex) {
radiant.offer(radiantIndex + n);
} else {
dire.offer(direIndex + n);
}
}
return !radiant.isEmpty() ? "Radiant" : "Dire";
}
}
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/dota2-senate/solution/dota2-can-yi-yuan-by-leetcode-solution-jb7l/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。