Flip Game
2016-07-06 14:52
92 查看
You are playing the following Flip Game with your friend: Given a string that contains only these two characters:
you and your friend take turns to flip two consecutive
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
思路:就是记录每个状态,记得翻转回来,然后再继续即可。
public class Solution {
public List<String> generatePossibleNextMoves(String s) {
List<String> list = new ArrayList<String>();
if(s == null || s.length() == 0) return list;
char[] chars = s.toCharArray();
for(int i=0; i<chars.length-1; i++){
if(chars[i]=='+' && chars[i+1]=='+'){
chars[i] = '-';
chars[i+1] = '-';
list.add(new String(chars));
chars[i] = '+';
chars[i+1] = '+';
}
}
return list;
}
}
+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:
[ "--++", "+--+", "++--" ]
思路:就是记录每个状态,记得翻转回来,然后再继续即可。
public class Solution {
public List<String> generatePossibleNextMoves(String s) {
List<String> list = new ArrayList<String>();
if(s == null || s.length() == 0) return list;
char[] chars = s.toCharArray();
for(int i=0; i<chars.length-1; i++){
if(chars[i]=='+' && chars[i+1]=='+'){
chars[i] = '-';
chars[i+1] = '-';
list.add(new String(chars));
chars[i] = '+';
chars[i+1] = '+';
}
}
return list;
}
}
相关文章推荐
- 证明
- safari 连接中有回车 兼容性问题
- 算法的空间复杂度
- Jquery
- GDAL 地图切片层级计算公式
- IntegerToEnglishWord
- Android保存用户名和密码
- C语言学习笔记(二)
- Memcache的安装与启动【小强测试培训班学员作品】
- 实习杂记(28):Conversion to Dalvik format failed with error问题所在
- 直插式电阻电容封装与尺寸图解
- sed处理url编码解码=== web日志的url处理
- [ActionScript 3.0] AS3.0 将图像的Alpha通道转换为黑白图像(分离ARGB方式)
- canvas
- 不再以讹传讹,GET和POST的真正区别
- 【算法】寻找1000000000(十亿)内素数并统计个数
- linux下redis安装
- 2016SDAU课程练习四1002 Problem B
- python socket编程
- selector用法中关于不同控件使用不同的state