[LeetCode294] Flip Game II

来源:互联网 发布:javascript validate 编辑:程序博客网 时间:2024/05/13 13:17

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 twoconsecutive "++" 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 "+--+".


class Solution {public:bool firstPlayWinning(string s) {return canWin(s);}bool canWin(string& s) {for(int i=0;i<s.size()-1;i++) {if(s[i]==s[i+1]&&s[i]=='+') {s[i]=s[i+1]='-';bool win=!canWin(s);s[i]=s[i+1]='+';if(win) return true;}}return false;}};


0 0