美文网首页
Flip Game II

Flip Game II

作者: Frank_Kivi | 来源:发表于2018-06-28 14:37 被阅读7次

    https://www.lintcode.com/problem/flip-game-ii/description

    public class Solution {
        /**
         * @param s: the given string
         * @return: if the starting player can guarantee a win
         */
        public boolean canWin(String s) {
            // write your code here
            boolean[] dp = new boolean[s.length()];
            for (int i = 0; i < dp.length; i++) {
                if (s.charAt(i) == '+') {
                    dp[i] = true;
                }
            }
            return treeWalk(dp);
        }
    
        private boolean treeWalk(boolean[] dp) {
            for (int i = 1; i < dp.length; i++) {
                if (dp[i - 1] && dp[i]) {
                    dp[i - 1] = false;
                    dp[i] = false;
                    if (!treeWalk(dp)) {//对方没有赢
                        dp[i - 1] = true;
                        dp[i] = true;
                        return true;
                    }
                    dp[i - 1] = true;
                    dp[i] = true;
                }
            }
            return false;
        }
    }
    

    相关文章

      网友评论

          本文标题:Flip Game II

          本文链接:https://www.haomeiwen.com/subject/vzmgyftx.html