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 twoconsecutive "++" 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 [].

链接: http://leetcode.com/problems/flip-game/

3/7/2017

 1 public class Solution {
 2     public List<String> generatePossibleNextMoves(String s) {
 3         StringBuilder sb = new StringBuilder(s);
 4         List<String> ret = new ArrayList<String>();
 5         boolean inPlus = false;
 6 
 7         for (int i = 0; i < s.length(); i++) {
 8             if (s.charAt(i) == '+') {
 9                 if (inPlus) {
10                     sb.setCharAt(i - 1, '-');
11                     sb.setCharAt(i, '-');
12                     ret.add(sb.toString());
13                     sb = new StringBuilder(s);                
14                 } else inPlus = true;
15             } else inPlus = false;
16         }
17         return ret;
18     }
19 }

 

posted @ 2017-03-08 06:15  panini  阅读(131)  评论(0编辑  收藏  举报