Flip Game I && II

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 [].

 1     public List<String> generatePossibleNextMoves(String s) {
 2         List<String> list = new ArrayList<String>();
 3
 4         if (s == null || s.length() < 2) return list;
 5
 6         for (int i = -1; (i = s.indexOf("++", i + 1)) >= 0;) {
 7             list.add(s.substring(0, i) + "--" + s.substring(i + 2));
 8         }
 9         return list;
10     }

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 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

1     public boolean canWin(String s) {
2         for (int i=-1; (i = s.indexOf("++", i+1)) >= 0; )
3             if (!canWin(s.substring(0, i) + "-" + s.substring(i + 2))) return true;
4         return false;
5     }
时间: 2024-10-19 17:06:25

Flip Game I && II的相关文章

294. Flip Game II

/* * 294. Flip Game II * 2016-7-2 by Mingyang * 这个题目我开始做的时候,还加了turn=1.-1来表示是哪个人走,还用了一个函数来表示什么时候不能走 * 其实都不用,这里如果没有可以走的了dfs的末尾会自动return false,另外,本轮是我走,我只要保证 * 下一轮return false就行了,我就return true,就是我走了以后,你下一轮无论怎么走,都不行 * 再次注意:题目的要求是determine if the starting

LeetCode Flip Game II

原题链接在这里:https://leetcode.com/problems/flip-game-ii/ 类似Flip Game. 若是有一段"++", 剩下的段和"--"组合 can not win, 那么返回true. 从头到尾试遍了没找到这么一段"++", 返回false. Time Complexity: exponential. AC Java: 1 public class Solution { 2 public boolean can

Flip Game II

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

Flip Game II -- LeetCode

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

第四篇 Flip Game II

1 public class Solution { 2 public boolean canWin(String s) { 3 if (s == null || s.length() <= 1) { 4 return false; 5 } 6 ArrayList<String> validMoves = getValidMoves(s); 7 if (validMoves.isEmpty()) { 8 return false; 9 } 10 for (String str : vali

[LeetCode] 349 Intersection of Two Arrays &amp; 350 Intersection of Two Arrays II

这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/intersection-of-two-arrays/description/ 350 Intersection of Two Arrays II:https://leetcode.com/problems/intersection-of-two-arrays-ii/description/ 题目&解法

使用华邦的SPI FLASH作为EPCS时固化NIOS II软件报错及解决方案

Altera器件有EPCS系列配置器件,其实,这些配置器件就是我们平时通用的SPIFlash,据AlteraFAE描述:"EPCS器件也是选用某家公司的SPIFlash,只是中间经过Altera公司的严格测试,所以稳定性及耐用性都超过通用的SPIFlash".就本人看来,半导体的稳定性问题绝大部分都是由本身设计缺陷造成的,而成熟的制造工艺不会造成产品的不稳定:并且,现在Altera的器件在读入配置数据发生错误时,可以重新读取SPIFlash里面的数据,所以在工艺的稳定性以及设计的可靠性

hdu 1207 汉诺塔II (DP+递推)

汉诺塔II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4529    Accepted Submission(s): 2231 Problem Description 经典的汉诺塔问题经常作为一个递归的经典例题存在.可能有人并不知道汉诺塔问题的典故.汉诺塔来源于印度传说的一个故事,上帝创造世界时作了三根金刚石柱子,在一根柱子上从下往

AC日记——小A和uim之大逃离 II 洛谷七月月赛

小A和uim之大逃离 II 思路: spfa: 代码: #include <bits/stdc++.h> using namespace std; #define INF 0x3f3f3f3f struct NodeType { int x,y,k; NodeType(int x_,int y_,int k_):x(x_),y(y_),k(k_){} NodeType(){} }; const int dx[5]={0,-1,0,1,0}; const int dy[5]={0,0,1,0,-