293 Flip Game

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:

[
  "--++",
  "+--+",
  "++--"
]
If there is no valid move, return an empty list [].
public class Solution {
    public List<String> generatePossibleNextMoves(String s) {
        List<String> res = new ArrayList<String>();
        if (s==null || s.length()<=1) return res;
        for (int i=0; i<s.length()-1; i++) {
            if (s.charAt(i)==‘+‘ &&s.charAt(i+1)==‘+‘)
                res.add(process(s, i));
        }
        return res;
    }

    public String process(String s, int i) {
        StringBuffer result = new StringBuffer();
        result.append(s.substring(0, i));
        result.append("--");
        result.append(s.substring(i+2));
        return result.toString();
    }
}

  

时间: 2024-11-15 05:59:42

293 Flip Game的相关文章

String题目集合

---恢复内容开始- 67. Add Binary Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Monaco; color: #4e9072 } 思路:这道题不好从后往前遍

leetcode 锁掉的题目清单

也刷leetcode, 先把锁掉的题目留备份好了: 156 Binary Tree Upside Down  [1] Problem: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tre

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

Sorted by frequency of problems that appear in real interviews.Last updated: October 2, 2017Google (214)534 Design TinyURL388 Longest Absolute File Path683 K Empty Slots340 Longest Substring with At Most K Distinct Characters681 Next Closest Time482

Leetcode 前 400 重点 250 题

这个重点题目是把Leetcode前400题进行精简,划分精简规则如下: 删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于简单题目(例:100题:Same Tree) 删除题意不同,代码基本相同题目(例:136 & 389,保留一个) 所有题目尽量保证客观公正,只是按大概率删除不常考题目,很多题目面经出现过, 但出现次数属于个位数或者只有一两家出现进行删除.所以如在面试中出现删除题目概不负责,这只是从概率上删除低频,简单题目. 旨在减轻大家的刷

20200108-20200112

244. Shortest Word Distance II - Medium 245. Shortest Word Distance III - Medium 246. Strobogrammatic Number - Easy 247. Strobogrammatic Number II - Medium method: recursion n =1 [0,1,8] n = 2 [11, 88, 69, 96] n = 3 [101, 111, 181, 808, 818, 888, 609

POJ 1753 Flip game状态压缩+广搜

题意有4*4的16个方格,每个方格有黑白两种颜色,每次点击方格后,被点击方格本身及其上下左右的方格都会改变颜色.给出一组状态,求将这组状态变为全白或者全黑至少需要点击几次.若无法达到,则输出Impossible. 样例输入bwwbbbwbbwwbbwww 样例输出4 思路每个方格只有黑白两种颜色,且只有16个方格,因此可以把每个状态看作一个16位的二进制数(状态压缩),2^16=25536,因此可以用int来保存状态.然后就是BFS暴搜,直到状态为0或者65535(全1)为止. 注意点65535

hdu 4146 Flip Game

Flip Game Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 1800    Accepted Submission(s): 589 Problem Description Flip game is played on a square N*N field with two-sided pieces placed on each

poj1753 Flip Game

Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the other one is black and each piece is lying either it's black or white side up. Each round you f