-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode649.java
More file actions
136 lines (127 loc) · 3.97 KB
/
LeetCode649.java
File metadata and controls
136 lines (127 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import java.util.Deque;
import java.util.LinkedList;
import java.util.Queue;
public class LeetCode649 {
public static void main(String[] args) {
// 输入:senate = "RD"
// 输出:"Radiant"
System.out.println(new Solution649_2().predictPartyVictory("RD"));
// 输入:senate = "RDD"
// 输出:"Dire"
System.out.println(new Solution649_2().predictPartyVictory("RDD"));
}
}
/**
* 构造一个环,遍历每个节点的时删除敌对的下一个节点直到只剩一种节点
*/
class Solution649_1 {
class Node {
char val;
Node next;
Node() {
}
Node(char value) {
this.val = value;
}
}
public String predictPartyVictory(String senate) {
int countR = 0;
int countD = 0;
for (char c : senate.toCharArray()) {
if (c == 'R') {
countR++;
} else {
countD++;
}
}
Node node1 = buildCycle(senate);
Node node2;
while (countD != 0 && countR != 0) {
node2 = node1;
while (node2.next.val == node1.val) {
node2 = node2.next;
}
node2.next = node2.next.next;
if (node1.val == 'R') {
// System.out.println("删除了一个D");
countD--;
} else {
// System.out.println("删除了一个R");
countR--;
}
node1 = node1.next;
}
return countD == 0 ? "Radiant" : "Dire";
}
public Node buildCycle(String senate) {
// 构造一个环
Node dummyNode = new Node();
Node curr = dummyNode;
for (char c : senate.toCharArray()) {
Node next = new Node(c);
curr.next = next;
curr = curr.next;
}
curr.next = dummyNode.next;
return dummyNode.next;
}
}
/**
* 上个解法最大的问题在于每次都要检索敌对元素的位置,因此考虑用两个队列分别存储两个势力
*/
class Solution649_2 {
public String predictPartyVictory(String senate) {
Deque<Integer> dequeR = new LinkedList<>();
Deque<Integer> dequeD = new LinkedList<>();
for (int i = 0; i < senate.length(); i++) {
if (senate.charAt(i) == 'R') {
dequeR.offer(i);
} else {
dequeD.offer(i);
}
}
// System.out.println(dequeR);
// System.out.println(dequeD);
int pre = -1;
while (!dequeD.isEmpty() && !dequeR.isEmpty()) {
int pD = dequeD.poll();
int pR = dequeR.poll();
int distD = pD > pre ? pD - pre : pD + senate.length() - pre;
int distR = pR > pre ? pR - pre : pR + senate.length() - pre;
if (distD < distR) {
pre = pD;
dequeD.offer(pD);
} else {
pre = pR;
dequeR.offer(pR);
}
}
return dequeD.isEmpty() ? "Radiant" : "Dire";
}
}
/**
* 上一个解法仍存在顺序确认麻烦的问题,考虑直接将遍历到的元素+N插回,而不是插入原值
*/
class Solution649_3 {
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";
}
}