1 Public classSolution {2 Public BooleanCanwin (String s) {3 if(s = =NULL|| S.length () <= 1) {4 return false;5 }6Arraylist<string> validmoves =getvalidmoves (s);7 if(Validmoves.isempty ()) {8 return false;9 }Ten for(String str:validmoves) { One if(!Canwin (str)) { A return true; - } - } the return false; - } - - PrivateArraylist<string>getvalidmoves (String s) { +arraylist<string> result =NewArrayList (); - for(inti = 0; I < S.length ()-1; i++) { + if(S.charat (i) = = ' + ' && s.charat (i + 1) = = ' + ') { A Char[] tmp =S.tochararray (); atTmp[i] = '-'; -Tmp[i + 1] = '-'; -Result.add (NewString (TMP)); - } - } - returnresult; in } -}
Google face by Topic
Before and Cong Meow review LinkedIn face through can I win, the same idea can also be used in this problem
This kind of rotating game problem can also be solved using the enumeration method, the key is the 11th row!canwin (str), indicating that the current selection of opponents must lose, then the current selection is the winning strategy
Fourth Flip Game II