字符串消消乐

来源:互联网 发布:淘宝服装拍摄灯光布置 编辑:程序博客网 时间:2024/04/28 21:28

遇到3个一样的就标记

 public static void test(String s) {        boolean modify = false;        int len = 0;        char[] re = new char[s.length()];        for (int i = 0; i < s.length(); i++) {            if (s.charAt(i) != '*') {                re[len++] = s.charAt(i);            }        }        char[] res = new char[len];        System.arraycopy(re, 0, res, 0, len);        char slow = 0;        char fast = 1;        while (fast < len) {            if (fast + 1 < len && res[fast] == res[fast + 1] && res[fast] == res[slow]) {                char temp = res[slow];                while (fast < len && res[fast] == temp) {                    modify = true;                    res[slow] = '*';                    res[fast] = '*';                    fast++;                }            }            slow = fast;            fast++;        }        if (modify) {            test(new String(res));        } else            System.out.println(Arrays.toString(res));    }
0 0
原创粉丝点击