LeetCode[10] - Flip Game

这个题目是很寂寞的. 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++;
        }
    }
}





最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 14,354评论 0 33
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,831评论 25 709
  • 2016年3月18号,开始了我的第一班航班,上海浦东飞往福州。在历经重重面试与为期两个月的乘务员培训,在那一...
    丁芳芳阅读 1,404评论 0 0
  • 1、基本思想:将数组中元素依次与其相邻元素比较,如果arr[j]>a[j ++]就调换位置2、特性:频繁改变数组位...
    GarinZhang阅读 1,605评论 0 0
  • 文/欣欣熳 -1- 前两天逛商场走到一家女装店门口,看到一个熟悉的身影,像是我以前的同事,Vicky, 但又感觉她...
    欣欣熳阅读 4,643评论 2 4