Flip Game I

Problem Description:

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 List<String> generatePossibleNextMoves(String s) {
List<String> list = new ArrayList<String>(); if (s == null || s.length() < ) return list; for (int i = -; (i = s.indexOf("++", i + )) >= ;) {
list.add(s.substring(, i) + "--" + s.substring(i + ));
}
return list;
}

Flip Game II

Problem Description:

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 determine if the starting player can guarantee a win.

For example, given s = "++++", return true. The starting player can guarantee a win by flipping the middle "++" to become "+--+".

Code is from: https://discuss.leetcode.com/topic/27287/short-java-ruby?show=64350

     public boolean canWin(String s) {
for (int i=-; (i = s.indexOf("++", i+)) >= ; )
if (!canWin(s.substring(, i) + "-" + s.substring(i + ))) return true;
return false;
}

Flip Game I && II的更多相关文章

  1. [Locked] Flip Game I & II

    Flip Game I You are playing the following Flip Game with your friend: Given a string that contains o ...

  2. [Swift]LeetCode969.煎饼排序 | Pancake Sorting

    Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.length, t ...

  3. [LeetCode] Flip Game II 翻转游戏之二

    You are playing the following Flip Game with your friend: Given a string that contains only these tw ...

  4. leetcode 293.Flip Game(lintcode 914) 、294.Flip Game II(lintcode 913)

    914. Flip Game https://www.cnblogs.com/grandyang/p/5224896.html 从前到后遍历,遇到连续两个'+',就将两个加号变成'-'组成新的字符串加 ...

  5. LeetCode Flip Game II

    原题链接在这里:https://leetcode.com/problems/flip-game-ii/ 题目: You are playing the following Flip Game with ...

  6. 294. Flip Game II

    题目: You are playing the following Flip Game with your friend: Given a string that contains only thes ...

  7. [Swift]LeetCode294. 翻转游戏之 II $ Flip Game II

    You are playing the following Flip Game with your friend: Given a string that contains only these tw ...

  8. Flip Game II -- LeetCode

    You are playing the following Flip Game with your friend: Given a string that contains only these tw ...

  9. [LeetCode] 294. Flip Game II 翻转游戏 II

    You are playing the following Flip Game with your friend: Given a string that contains only these tw ...

随机推荐

  1. Linux大文件已删除,但df查看已使用的空间并未减少解决

    在我的生活当中遇到磁盘快满了,这时候准备去删除一些大文件 于是我使用ncdu 查看了一下当前系统占用资源比较多的是那些文件,结果一看是elasticsearch的日志文件,好吧,竟然找到源头了,那就把 ...

  2. strcmp的实现

    注意,*str1++和*str2++最好不要写在while判断里,否则需要在return前再*str1-1,和*str2-1. int strcmp(const char *str1,const ch ...

  3. 【转】Eclipse下导入外部jar包的3种方式

    我们在用Eclipse开发程序的时候,经常要用到第三方jar包.引入jar包不是一个小问题,由于jar包位置不清楚,而浪费时间.下面配图说明3种Eclipse引入jar包的方式.   1.最常用的普通 ...

  4. BZOJ1086 [SCOI2005]王室联邦

    Description “余”人国的国王想重新编制他的国家.他想把他的国家划分成若干个省,每个省都由他们王室联邦的一个成 员来管理.他的国家有n个城市,编号为1..n.一些城市之间有道路相连,任意两个 ...

  5. groovy-真值

    Boolean expressions Groovy支持标准的条件运算符的布尔表达式: 1 def a = true 2 def b = true 3 def c = false 4 assert a ...

  6. groovy-位运算

    从Groovy 1.0 beta 10开始,Groovy支持位运算:<<. >>, >>>, |, &, ^, and ~. 下表列出了位运算的操作符 ...

  7. $root knockout

    http://www.cnblogs.com/rohelm/p/3209757.html 以列表方式呈现数据  处理以数组形式储存的多条数据,要先认识foreach.在ViewModel定义一个Jav ...

  8. if,switch,do,while,for实例

    1.#include <stdio.h>void f1(int i){    if( i < 6 )    {        printf("Failed!\n" ...

  9. Spring学习6-Spring整合Struts2

    一.Spring为什么要整合Struts2     Struts2与Spring进行整合的根本目的就是要让 Spring为Struts2的Action注入所需的资源对象,它们整合的原理则是只要导入了s ...

  10. try、catch 和 throw 语句 (了解)

    C++ 异常使用 try.catch 和 throw 关键字. 引发表达式指示错误或异常情况. 可以将任何类型的对象用作引发表达式的操作数. 此对象通常用于传达有关错误的信息. 通常,应使用在标准库中 ...