*LeetCode-Flip Game II

backtracking 每次翻两个 然后向下递归 但是注意要取下次递归的返回值 因为假如对手一定输了 那么自己才一定赢了

public class Solution {
    public boolean canWin(String s) {
        for ( int i = 0; i < s.length() - 1; i ++ ){
            if ( s.charAt ( i ) == '+' && s.charAt( i + 1 ) == '+' ){
                StringBuilder sb = new StringBuilder ( s );
                sb.setCharAt ( i , '-');
                sb.setCharAt ( i + 1 ,'-');
                if ( !canWin ( sb.toString() ) )
                    return true;
            }
        }
        return false;
    }
}


你可能感兴趣的:(*LeetCode-Flip Game II)