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 f5726c1

Browse files
solves #293: flip game in java
1 parent ac58f56 commit f5726c1

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

β€ŽREADME.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@
256256
| 290 | [Word Pattern](https://leetcode.com/problems/word-pattern) | [![Java](assets/java.png)](src/WordPattern.java) [![Python](assets/python.png)](python/word_pattern.py) | |
257257
| 291 | πŸ”’ [Word Pattern II](https://leetcode.com/problems/word-pattern-ii) | | |
258258
| 292 | [Nim Game](https://leetcode.com/problems/nim-game) | [![Java](assets/java.png)](src/NimGame.java) [![Python](assets/python.png)](python/nim_game.py) | |
259-
| 293 | πŸ”’ [Flip Game](https://leetcode.com/problems/flip-game) | | |
259+
| 293 | πŸ”’ [Flip Game](https://leetcode.com/problems/flip-game) | [![Java](assets/java.png)](src/FlipGame.java) | |
260260
| 294 | πŸ”’ [Flip Game II](https://leetcode.com/problems/flip-game-ii) | | |
261261
| 298 | πŸ”’ [Binary Tree Longest Consecutive Sequence](https://leetcode.com/problems/binary-tree-longest-consecutive-sequence) | | |
262262
| 299 | [Bulls and Cows](https://leetcode.com/problems/bulls-and-cows) | [![Java](assets/java.png)](src/BullsAndCows.java) [![Python](assets/python.png)](python/bulls_and_cows.py) | |

β€Žsrc/FlipGame.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// https://leetcode.com/problems/flip-game
2+
// N = |currentState|
3+
// T: O(N^2)
4+
// S: O(N^2)
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
public class FlipGame {
10+
public List<String> generatePossibleNextMoves(String currentState) {
11+
final List<String> result = new ArrayList<>();
12+
for (int i = 0 ; i < currentState.length() - 1 ; i++) {
13+
if (currentState.charAt(i) == '+' && currentState.charAt(i + 1) == '+') {
14+
result.add(flip(currentState, i));
15+
}
16+
}
17+
return result;
18+
}
19+
20+
private static String flip(String state, int index) {
21+
return state.substring(0, index) + "--" + state.substring(index + 2);
22+
}
23+
}

0 commit comments

Comments
(0)

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /