美文网首页
Flip Game II

Flip Game II

作者: BLUE_fdf9 | 来源:发表于2018-11-13 11:21 被阅读0次

题目
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 two consecutive "++" 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.

答案

class Solution {
    public boolean canWin(String s) {
        List<String> next_steps = generatePossibleNextMoves(s);
        for(int i = 0; i < next_steps.size(); i++) {
            boolean ret = canWin(next_steps.get(i));
            if(ret == false) return true;
        }
        return false;
    }
    
    public List<String> generatePossibleNextMoves(String s) {
        List<String> list = new ArrayList<>();
        char[] ss = s.toCharArray();
        for(int i = 0; i < s.length(); i++) {
            // Replace current '++' with '--'
            if(s.charAt(i) == '+' && (i+1 < s.length() && s.charAt(i+1) == '+')) {
                String str = s.substring(0, i) + "--" + s.substring(i + 2);
                list.add(str);
            }
        }
        return list;
    }
    
}

相关文章

  • Flip Game II

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

  • Flip Game II

    题目You are playing the following Flip Game with your frien...

  • CUC-SUMMER-2-J

    J - Flip Game POJ - 1753 Flip game is played on a rectang...

  • 294. Flip Game II

    Description You are playing the following Flip Game with ...

  • 294. Flip Game II

    You are playing the following Flip Game with your friend:...

  • LeetCode[15] - Flip Game II

    这个题目李特是屌炸天的。我飞了九牛二虎之力(路子对),但是代码写的七荤八素,好长好长好长好长的。结果正解,三四行就...

  • 294. Flip Game II

    You are playing the following Flip Game with your friend:...

  • LintCode

    1042 Toeplitz Matrix Flip Game

  • 293. Flip Game

    Description You are playing the following Flip Game with ...

  • Flip Game

    题目You are playing the following Flip Game with your frien...

网友评论

      本文标题:Flip Game II

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