
贪心算法通过循环 把问题 从小部分的最优解决变为 整体的 最优解决
class Solution {
public String predictPartyVictory(String senate) {
Queue<Integer> r = new LinkedList<Integer>();
Queue<Integer> d = new LinkedList<Integer>();
int n = senate.length();
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 rpoll = r.poll();
int dpoll = d.poll();
if(rpoll < dpoll ){
//为了区分轮次,所有必须要加n,这也是该题的难点
r.offer(rpoll + n);
}else{
d.offer(dpoll + n);
}
}
if(r.isEmpty()){
return "Dire";
}else{
return "Radiant";
}
}
}```
4409

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



