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 6c3ae56

Browse files
committed
feat: add solutions to lc problem: No.0293
No.0293.Flip Game
1 parent c906af5 commit 6c3ae56

File tree

6 files changed

+170
-57
lines changed

6 files changed

+170
-57
lines changed

‎solution/0200-0299/0293.Flip Game/README.md‎

Lines changed: 60 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@
4949

5050
```python
5151
class Solution:
52-
def generatePossibleNextMoves(self, s: str) -> List[str]:
53-
ifnot s orlen(s) <2:
54-
return []
55-
n =len(s)
56-
res = []
57-
for i inrange(n -1):
58-
if s[i] =='+'and s[i +1] =='+':
59-
res.append(s[:i] +"--"+s[i + 2:])
60-
return res
52+
def generatePossibleNextMoves(self, currentState: str) -> List[str]:
53+
s =list(currentState)
54+
ans = []
55+
for i, c inenumerate(s[:-1]):
56+
if c =="+"and s[i +1] =="+":
57+
s[i] = s[i +1] ="-"
58+
ans.append("".join(s))
59+
s[i] =s[i + 1] ="+"
60+
return ans
6161
```
6262

6363
### **Java**
@@ -66,22 +66,63 @@ class Solution:
6666

6767
```java
6868
class Solution {
69-
public List<String> generatePossibleNextMoves(String s) {
70-
int n;
71-
if (s == null || (n = s.length()) < 2) return Collections.emptyList();
72-
List<String> res = new ArrayList<>();
73-
for (int i = 0; i < n - 1; ++i) {
74-
if (s.charAt(i) == '+' && s.charAt(i + 1) == '+') {
75-
StringBuilder sb = new StringBuilder(s);
76-
sb.replace(i, i + 2, "--");
77-
res.add(sb.toString());
69+
public List<String> generatePossibleNextMoves(String currentState) {
70+
char[] cs = currentState.toCharArray();
71+
List<String> ans = new ArrayList<>();
72+
for (int i = 0; i < cs.length - 1; ++i) {
73+
if (cs[i] == '+' && cs[i + 1] == '+') {
74+
cs[i] = '-';
75+
cs[i + 1] = '-';
76+
ans.add(String.valueOf(cs));
77+
cs[i] = '+';
78+
cs[i + 1] = '+';
7879
}
7980
}
80-
return res;
81+
return ans;
8182
}
8283
}
8384
```
8485

86+
### **C++**
87+
88+
```cpp
89+
class Solution {
90+
public:
91+
vector<string> generatePossibleNextMoves(string currentState) {
92+
vector<string> ans;
93+
for (int i = 0; i < currentState.size() - 1; ++i)
94+
{
95+
if (currentState[i] == '+' && currentState[i + 1] == '+')
96+
{
97+
currentState[i] = '-';
98+
currentState[i + 1] = '-';
99+
ans.push_back(currentState);
100+
currentState[i] = '+';
101+
currentState[i + 1] = '+';
102+
}
103+
}
104+
return ans;
105+
}
106+
};
107+
```
108+
109+
### **Go**
110+
111+
```go
112+
func generatePossibleNextMoves(currentState string) []string {
113+
ans := []string{}
114+
cs := []byte(currentState)
115+
for i, c := range cs[1:] {
116+
if c == '+' && cs[i] == '+' {
117+
cs[i], cs[i+1] = '-', '-'
118+
ans = append(ans, string(cs))
119+
cs[i], cs[i+1] = '+', '+'
120+
}
121+
}
122+
return ans
123+
}
124+
```
125+
85126
### **...**
86127

87128
```

‎solution/0200-0299/0293.Flip Game/README_EN.md‎

Lines changed: 60 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,37 +41,78 @@
4141

4242
```python
4343
class Solution:
44-
def generatePossibleNextMoves(self, s: str) -> List[str]:
45-
ifnot s orlen(s) <2:
46-
return []
47-
n =len(s)
48-
res = []
49-
for i inrange(n -1):
50-
if s[i] =='+'and s[i +1] =='+':
51-
res.append(s[:i] +"--"+s[i + 2:])
52-
return res
44+
def generatePossibleNextMoves(self, currentState: str) -> List[str]:
45+
s =list(currentState)
46+
ans = []
47+
for i, c inenumerate(s[:-1]):
48+
if c =="+"and s[i +1] =="+":
49+
s[i] = s[i +1] ="-"
50+
ans.append("".join(s))
51+
s[i] =s[i + 1] ="+"
52+
return ans
5353
```
5454

5555
### **Java**
5656

5757
```java
5858
class Solution {
59-
public List<String> generatePossibleNextMoves(String s) {
60-
int n;
61-
if (s == null || (n = s.length()) < 2) return Collections.emptyList();
62-
List<String> res = new ArrayList<>();
63-
for (int i = 0; i < n - 1; ++i) {
64-
if (s.charAt(i) == '+' && s.charAt(i + 1) == '+') {
65-
StringBuilder sb = new StringBuilder(s);
66-
sb.replace(i, i + 2, "--");
67-
res.add(sb.toString());
59+
public List<String> generatePossibleNextMoves(String currentState) {
60+
char[] cs = currentState.toCharArray();
61+
List<String> ans = new ArrayList<>();
62+
for (int i = 0; i < cs.length - 1; ++i) {
63+
if (cs[i] == '+' && cs[i + 1] == '+') {
64+
cs[i] = '-';
65+
cs[i + 1] = '-';
66+
ans.add(String.valueOf(cs));
67+
cs[i] = '+';
68+
cs[i + 1] = '+';
6869
}
6970
}
70-
return res;
71+
return ans;
7172
}
7273
}
7374
```
7475

76+
### **C++**
77+
78+
```cpp
79+
class Solution {
80+
public:
81+
vector<string> generatePossibleNextMoves(string currentState) {
82+
vector<string> ans;
83+
for (int i = 0; i < currentState.size() - 1; ++i)
84+
{
85+
if (currentState[i] == '+' && currentState[i + 1] == '+')
86+
{
87+
currentState[i] = '-';
88+
currentState[i + 1] = '-';
89+
ans.push_back(currentState);
90+
currentState[i] = '+';
91+
currentState[i + 1] = '+';
92+
}
93+
}
94+
return ans;
95+
}
96+
};
97+
```
98+
99+
### **Go**
100+
101+
```go
102+
func generatePossibleNextMoves(currentState string) []string {
103+
ans := []string{}
104+
cs := []byte(currentState)
105+
for i, c := range cs[1:] {
106+
if c == '+' && cs[i] == '+' {
107+
cs[i], cs[i+1] = '-', '-'
108+
ans = append(ans, string(cs))
109+
cs[i], cs[i+1] = '+', '+'
110+
}
111+
}
112+
return ans
113+
}
114+
```
115+
75116
### **...**
76117

77118
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
vector<string> generatePossibleNextMoves(string currentState) {
4+
vector<string> ans;
5+
for (int i = 0; i < currentState.size() - 1; ++i)
6+
{
7+
if (currentState[i] == '+' && currentState[i + 1] == '+')
8+
{
9+
currentState[i] = '-';
10+
currentState[i + 1] = '-';
11+
ans.push_back(currentState);
12+
currentState[i] = '+';
13+
currentState[i + 1] = '+';
14+
}
15+
}
16+
return ans;
17+
}
18+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func generatePossibleNextMoves(currentState string) []string {
2+
ans := []string{}
3+
cs := []byte(currentState)
4+
for i, c := range cs[1:] {
5+
if c == '+' && cs[i] == '+' {
6+
cs[i], cs[i+1] = '-', '-'
7+
ans = append(ans, string(cs))
8+
cs[i], cs[i+1] = '+', '+'
9+
}
10+
}
11+
return ans
12+
}
Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
class Solution {
2-
public List<String> generatePossibleNextMoves(String s) {
3-
int n;
4-
if (s == null || (n = s.length()) < 2) return Collections.emptyList();
5-
List<String> res = new ArrayList<>();
6-
for (int i = 0; i < n - 1; ++i) {
7-
if (s.charAt(i) == '+' && s.charAt(i + 1) == '+') {
8-
StringBuilder sb = new StringBuilder(s);
9-
sb.replace(i, i + 2, "--");
10-
res.add(sb.toString());
2+
public List<String> generatePossibleNextMoves(String currentState) {
3+
char[] cs = currentState.toCharArray();
4+
List<String> ans = new ArrayList<>();
5+
for (int i = 0; i < cs.length - 1; ++i) {
6+
if (cs[i] == '+' && cs[i + 1] == '+') {
7+
cs[i] = '-';
8+
cs[i + 1] = '-';
9+
ans.add(String.valueOf(cs));
10+
cs[i] = '+';
11+
cs[i + 1] = '+';
1112
}
1213
}
13-
return res;
14+
return ans;
1415
}
1516
}
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
class Solution:
2-
def generatePossibleNextMoves(self, s: str) -> List[str]:
3-
ifnotsorlen(s) <2:
4-
return []
5-
n=len(s)
6-
res= []
7-
foriinrange(n-1):
8-
ifs[i] =='+'ands[i+1] =='+':
9-
res.append(s[:i] +"--"+s[i + 2 :])
10-
return res
2+
def generatePossibleNextMoves(self, currentState: str) -> List[str]:
3+
s=list(currentState)
4+
ans= []
5+
fori, cinenumerate(s[:-1]):
6+
ifc=="+"ands[i+1] =="+":
7+
s[i] =s[i+1] ="-"
8+
ans.append("".join(s))
9+
s[i] =s[i + 1] ="+"
10+
return ans

0 commit comments

Comments
(0)

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