Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8b1e22b

Browse files
authored
feat: add rust solution to lc problem: No.0649 (#1628)
No.0649.Dota2 Senate
1 parent 19bb84f commit 8b1e22b

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

‎solution/0600-0699/0649.Dota2 Senate/README.md‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,41 @@ function predictPartyVictory(senate: string): string {
218218
}
219219
```
220220

221+
### **Rust**
222+
223+
```rust
224+
impl Solution {
225+
pub fn predict_party_victory(senate: String) -> String {
226+
let mut qr = std::collections::VecDeque::new();
227+
let mut qd = std::collections::VecDeque::new();
228+
let n = senate.len();
229+
for i in 0..n {
230+
if let Some(char) = senate.chars().nth(i) {
231+
if char == 'R' {
232+
qr.push_back(i);
233+
} else {
234+
qd.push_back(i);
235+
}
236+
}
237+
}
238+
239+
while !qr.is_empty() && !qd.is_empty() {
240+
let front_qr = qr.pop_front().unwrap();
241+
let front_qd = qd.pop_front().unwrap();
242+
if front_qr < front_qd {
243+
qr.push_back(front_qr + n);
244+
} else {
245+
qd.push_back(front_qd + n);
246+
}
247+
}
248+
if qr.is_empty() {
249+
return "Dire".to_string();
250+
}
251+
"Radiant".to_string()
252+
}
253+
}
254+
```
255+
221256
### **...**
222257

223258
```

‎solution/0600-0699/0649.Dota2 Senate/README_EN.md‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,41 @@ function predictPartyVictory(senate: string): string {
197197
}
198198
```
199199

200+
### **Rust**
201+
202+
```rust
203+
impl Solution {
204+
pub fn predict_party_victory(senate: String) -> String {
205+
let mut qr = std::collections::VecDeque::new();
206+
let mut qd = std::collections::VecDeque::new();
207+
let n = senate.len();
208+
for i in 0..n {
209+
if let Some(char) = senate.chars().nth(i) {
210+
if char == 'R' {
211+
qr.push_back(i);
212+
} else {
213+
qd.push_back(i);
214+
}
215+
}
216+
}
217+
218+
while !qr.is_empty() && !qd.is_empty() {
219+
let front_qr = qr.pop_front().unwrap();
220+
let front_qd = qd.pop_front().unwrap();
221+
if front_qr < front_qd {
222+
qr.push_back(front_qr + n);
223+
} else {
224+
qd.push_back(front_qd + n);
225+
}
226+
}
227+
if qr.is_empty() {
228+
return "Dire".to_string();
229+
}
230+
"Radiant".to_string()
231+
}
232+
}
233+
```
234+
200235
### **...**
201236

202237
```
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
impl Solution {
2+
pub fn predict_party_victory(senate: String) -> String {
3+
let mut qr = std::collections::VecDeque::new();
4+
let mut qd = std::collections::VecDeque::new();
5+
let n = senate.len();
6+
for i in 0..n {
7+
if let Some(char) = senate.chars().nth(i) {
8+
if char == 'R' {
9+
qr.push_back(i);
10+
} else {
11+
qd.push_back(i);
12+
}
13+
}
14+
}
15+
16+
while !qr.is_empty() && !qd.is_empty() {
17+
let front_qr = qr.pop_front().unwrap();
18+
let front_qd = qd.pop_front().unwrap();
19+
if front_qr < front_qd {
20+
qr.push_back(front_qr + n);
21+
} else {
22+
qd.push_back(front_qd + n);
23+
}
24+
}
25+
if qr.is_empty() {
26+
return "Dire".to_string();
27+
}
28+
"Radiant".to_string()
29+
}
30+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /