*LeetCode-Flip Game II

来源:互联网 发布:添加字段sql 编辑:程序博客网 时间:2024/05/24 02:28

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;    }}


0 0
原创粉丝点击