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. 在cmd中运行带包名的java程序

    例: 在 d 盘中的 zh.java 文件,zh.java文件中有package com.fanShe.....; 则命令是  javac -d . zh.java 要在中间加入 -d . 后面运行的 ...

  2. [转]SQL注入攻防入门详解

    原文地址:http://www.cnblogs.com/heyuquan/archive/2012/10/31/2748577.html =============安全性篇目录============ ...

  3. Google-解决在调试页面或者js时总是提示烦恼的断点问题

    按F12键,然后切换到Source标签,看底下的那个跟暂停一样的图标是不是变成蓝色或紫色了? 如果是蓝色或者紫色,则把他切换到“灰色”状态(点击图标就会切换成不同的状态.或者可能是其他颜色状态),如下 ...

  4. POJ 2828 Buy Tickets

    Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must get ...

  5. MVC简介

    全名是Model View Controller,模型-视图-控制器,一种软件设计典范,用一种业务逻辑.数据.界面显示分离的方法组织代码,将业务逻辑聚集到一个部件里面,在改进和个性化定制界面及用户交互 ...

  6. php返回json数据简单实例

    <?php include './include/conn.php'; //数据库链接文件 $sql_notice = mysql_query('SELECT * FROM gg_notice ...

  7. 锋利的jQuery-4--trigger()和triggerHandler()

    trigger()方法触发事件后,会执行浏览器默认操作. $("input").trigger("focus") 以上的代码不仅会执行input绑定的focus ...

  8. Change ICON of MFC Application and Dialog

    Change ICON of MFC Application and Dialoghttp://www.codeproject.com/Tips/406870/Change-ICON-of-MFC-A ...

  9. C++ 的语言杂谈(一)--C++不是新手友好的

    C++的语言品味是独特的,喜欢的人特别喜欢,讨厌的人特别讨厌.虽然Bjane Stroustrup不断地宣称C++的发展方向是新手友好的,但实际上对新手来说,最重要的还是有强大方便的标准库可以使用(像 ...

  10. javaScript模块化规范ADM与CMD

    模块化:模块化是指在解决某一个复杂问题时,依照一种分类的思维把问题进行系统性的分解处理,可以想象一个巨大的系统代码,被整合优化分割成逻辑性很强的模块时,对于软件是一种何等意义的存在. 模块化系统所必须 ...