美文网首页李特抠得LeetCode
LeetCode[10] - Flip Game

LeetCode[10] - Flip Game

作者: 土汪 | 来源:发表于2015-10-31 00:26 被阅读209次

这个题目是很寂寞的. 2 pointer可以做, 在网上又搜了一下,貌似可以有很多牛逼的优化,我暂时还没去看。
很郁闷的就是条件不明,原来只需要从'++'转到'--'的情况,反过来没必要关注...搞了我半天啊

/*
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 compute all possible states of the string after one valid move.

For example, given s = "++++", after one move, it may become one of the following states:

[
  "--++",
  "+--+",
  "++--"
]
*/

/*
Thoughts:
Two pointers to check if p1 and p2 match target patern. If so, add.

Need to ask: are we only looking to change to '--' from '++'?
*/
public class Solution {
    public static List<String> generatePossibleNextMoves(String s) {
        List<String> rst = new ArrayList<String>();
        if (s == null || s.length() < 1) {
            return rst;
        }
        char[] arr = s.toCharArray();
        search('+','-',arr,rst);
        return rst;
    }

    public static void search(char target, char replace, char[] arr, List<String> rst) {
        int p1 = 0;
        int p2 = 1;
        while (p2 <= arr.length - 1) {
            if (arr[p1] == target && arr[p2] == target) {
                arr[p1] = replace;
                arr[p2] = replace;
                rst.add(new String(arr));
                arr[p1] = target;
                arr[p2] = target;
            }
            p1++;
            p2++;
        }
    }
}





相关文章

  • LeetCode[10] - Flip Game

    这个题目是很寂寞的. 2 pointer可以做, 在网上又搜了一下,貌似可以有很多牛逼的优化,我暂时还没去看。很郁...

  • CUC-SUMMER-2-J

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

  • LeetCode[15] - Flip Game II

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

  • LintCode

    1042 Toeplitz Matrix Flip Game

  • 293. Flip Game

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

  • 294. Flip Game II

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

  • Flip Game

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

  • 294. Flip Game II

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

  • 【LEETCODE】模拟面试-294.Flip Game II

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

  • 294. Flip Game II

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

网友评论

    本文标题:LeetCode[10] - Flip Game

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