-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlipGame.cc
More file actions
60 lines (49 loc) · 1.38 KB
/
Copy pathFlipGame.cc
File metadata and controls
60 lines (49 loc) · 1.38 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
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <sstream>
#include <iostream>
#include <vector>
using namespace std;
/*
[LeetCode] Flip Game
Problem Description:
You are playing the following Flip Game with your friend:
Given a string that contains only these two characters: + and -,
you and your friend take turns to
flip two consecutive "++" into "--". The game ends when a person can no longer
make a move and therefore the other person will be the winner.
Write a function to compute all possible states of the string after one valid
move.
For example, given s = "++++", after one move, it may become one of the
following states:
[
"--++",
"+--+",
"++--"
]
If there is no valid move, return an empty list [].
The idea is quite straightforward: just traverse s and each time when we see two
consecutive+s, convert them to -s and add the resulting string to the final
result moves. But remember to recover the string after that.
The C++ code is as follows.
*/
class Solution {
public:
vector<string> generatePossibleNextMoves(string s) {
vector<string> moves;
int n = s.length();
for (int i = 0; i < n - 1; i++) {
if (s[i] == '+' && s[i + 1] == '+') {
s[i] = s[i + 1] = '-';
moves.push_back(s);
s[i] = s[i + 1] = '+';
}
}
return moves;
}
};
int main(int argc, char const *argv[]) {
/* code */
return 0;
}