Flip Game I && II
2016-07-30 01:17
316 查看
Flip Game I
Problem Description:
You are playing the following Flip Game with your friend: Given a string that contains only these two characters:
Write a function to compute all possible states of the string after one valid move.
For example, given
If there is no valid move, return an empty list
Write a function to determine if the starting player can guarantee a win.
For example, given
Code is from: https://discuss.leetcode.com/topic/27287/short-java-ruby?show=64350
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
[].
public List<String> generatePossibleNextMoves(String s) { List<String> list = new ArrayList<String>(); if (s == null || s.length() < 2) return list; for (int i = -1; (i = s.indexOf("++", i + 1)) >= 0;) { list.add(s.substring(0, i) + "--" + s.substring(i + 2)); } return list; }
Flip Game II
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 determine if the starting player can guarantee a win.
For example, given
s = "++++", return true. The starting player can guarantee a win by flipping the middle
"++"to become
"+--+".
Code is from: https://discuss.leetcode.com/topic/27287/short-java-ruby?show=64350
public boolean canWin(String s) { for (int i=-1; (i = s.indexOf("++", i+1)) >= 0; ) if (!canWin(s.substring(0, i) + "-" + s.substring(i + 2))) return true; return false; }
相关文章推荐
- 2013年7月20号: Flip Game&&The Pilots Brothers' refrigerator&&Coin Change&&Discrete Function&&Football Gambling II
- [Locked] Flip Game I & II
- POJ 1753 Flip Game
- Jump Game I&&II——入门级贪心算法
- Leetcode Flip Game II
- [LeetCode] Flip Game II
- DFS.枚舉::poj1753 flip game && poj2965 the Pilots Brothers'refrigerator
- Leetcode: Flip Game II
- 【leetcode】Jump Game I & II (hard)
- LeetCode Jump Game && Jump GameII
- [LeetCode294]Flip Game II
- 【DP&数组】Jump Game II
- Leetcode | Jump Game I && II
- Valid Sudoku & Subsets I&&II & Flatten Binary Tree to Linked List & Jump Game I&&II
- 【leetcode】Jump Game I & II
- leetcode jump game I&&II
- NYOJ 970 Yougth's Game II
- LeetCode:Jump Game I && II
- Jump Game &Jump Game II
- 294. Flip Game II