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. Hibernate-入门教程

    首先了解hibernate的目录结构 . +lib antlr.jar cglib-full.jar asm.jar asm-attrs.jars commons-collections.jar co ...

  2. 【kAri OJ 616】Asce的树

    时间限制 1000 ms 内存限制 65536 KB 题目描述 作为一个东北大老爷们,大A熊以力气大著称,现在有一颗半径为r的树,剖面图如黑色的圆,大A熊决定搬几个半径为R的圆柱形桶将其围住,剖面图如 ...

  3. 【bzoj1013】 JSOI2008—球形空间产生器sphere

    www.lydsy.com/JudgeOnline/problem.php?id=1013 (题目链接) 题意 有一个n维的球体,给出球上n+1个点,求出圆心. Solution 题中给出了对于n维空 ...

  4. Mybatis的ResultMap的使用

    本篇文章通过一个实际工作中遇到的例子开始吧: 工程使用Spring+Mybatis+Mysql开发.具体的业务逻辑很重,对象之间一层一层的嵌套.和数据库表对应的是大量的model类,而和前端交互的是V ...

  5. 学习笔记-Kuaihu(仿知乎日报)

    本文目的:由于第一次学习较为完整的项目,故作记录以系统地整理APP开发知识 先看看整个项目结构: activity, fragment, 不用说了.可以看做MVC中的controller db, 存储 ...

  6. 千万不要误用 java 中的 HashCode 方法

    刚才debug追堆栈的时候发现一个很奇怪的问题 我用IE8和Google的浏览器访问同一个地址 Action的 scope="session" 也设置了 而且两个浏览器提交的参数m ...

  7. tomcat管理员配置

    纸上得来终觉浅,绝知此事要躬行 博客园 首页 新闻 新随笔 联系 管理 随笔- 458  文章- 0  评论- 38  Tomcat的Manager显示403 Access Denied   管理to ...

  8. linux3

    第一课:date +%Y-%m-%d 显示日期date +%H:%M 显示小时分钟date 显示日期 vi /etc/sysconfig/network-scripts/ifcfg-eth0 网卡配置 ...

  9. MyBatis查询传一个参数时报错:There is no getter for property named 'sleevetype' in 'class java.lang.Integer

    用MyBatis进行查询,传入参数只有一个时(非Map)如int,报错 There is no getter for property named 'sleevetype' in 'class jav ...

  10. JS面试题及答案总结

    1. 截取字符串abcdefg的efg  <div id="test">abcdefg</div> var mytext=document.getEleme ...