zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    路由协议
    TDD一示范例
    leetcode-36 + this may be useful when development is performed under newer sdk version
    leetcode-35
    TCP扫盲1
    UDP扫盲
    leetcode-34
    leetcode-33
    leetcode-32
    mysql非常全的和完整的总结
  • 原文地址:https://www.cnblogs.com/panini/p/6517664.html
Copyright © 2011-2022 走看看