293. Flip Game
题目:
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 twoconsecutive "++" 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 [].
链接: http://leetcode.com/problems/flip-game/
题解:
把"++"flip成"--"。把输入String转化为char[]就很好操作了。 其实用String也好操作,看到Stefan写了一个4行的,很精彩。
Time Complexity - O(n), Space Complexity - O(n)。
public class Solution {
public List<String> generatePossibleNextMoves(String s) {
List<String> res = new ArrayList<>();
char[] arr = s.toCharArray();
for(int i = 1; i < s.length(); i++) {
if(arr[i] == '+' && arr[i - 1] == '+') {
arr[i] = '-';
arr[i - 1] = '-';
res.add(String.valueOf(arr));
arr[i] = '+';
arr[i - 1] = '+';
}
}
return res;
}
}
二刷:
题目的意思是,record all states after one valid move, 所以我们只需要flip一次。 先把String转换为数组,从1开始到最后,把两个连续的'+'变为'-',记录下这个结果,再backtracking把那两个'-'改回去,接着计算下面的结果。遍历完一次数组之后就可以了。
Java:
Time Complexity - O(n), Space Complexity - O(n)。
public class Solution {
public List<String> generatePossibleNextMoves(String s) {
List<String> res = new ArrayList<>();
if (s == null || s.length() < 2) {
return res;
}
char[] arr = s.toCharArray();
for (int i = 1; i < arr.length; i++) {
if (arr[i] == '+' && arr[i - 1] == '+') {
arr[i] = '-';
arr[i - 1] = '-';
res.add(String.valueOf(arr));
arr[i] = '+';
arr[i - 1] = '+';
}
}
return res;
}
}
三刷:
跟之前一样
Java:
public class Solution {
public List<String> generatePossibleNextMoves(String s) {
List<String> res = new ArrayList<>();
if (s == null || s.length() < 2) return res;
char[] str = s.toCharArray();
for (int i = 1; i < str.length; i++) {
if (str[i] == '+' && str[i - 1] == '+') {
str[i - 1] = '-';
str[i] = '-';
res.add(new String(str));
str[i - 1] = '+';
str[i] = '+';
}
}
return res;
}
}
Reference:
https://leetcode.com/discuss/64248/4-lines-in-java
https://leetcode.com/discuss/64335/simple-solution-in-java
293. Flip Game的更多相关文章
- 293. Flip Game只翻转一步的加减号翻转游戏
[抄题]: You are playing the following Flip Game with your friend: Given a string that contains only th ...
- LeetCode 293. Flip Game
原题链接在这里:https://leetcode.com/problems/flip-game/description/ 题目: You are playing the following Flip ...
- [LeetCode] 293. Flip Game 翻转游戏
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
- leetcode 293.Flip Game(lintcode 914) 、294.Flip Game II(lintcode 913)
914. Flip Game https://www.cnblogs.com/grandyang/p/5224896.html 从前到后遍历,遇到连续两个'+',就将两个加号变成'-'组成新的字符串加 ...
- [LC] 293. Flip Game
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
- 【LeetCode】293. Flip Game 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- <String> 186 293 294 249
186. Reverse Words in a String II 先反转整个字符串,再反转每个单词(调整顺序也可以) 反转单词的时候:当 j 指到最后一个字符的时候,或者 j 的下一个指向空格,则反 ...
- [LeetCode] 294. Flip Game II 翻转游戏 II
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
随机推荐
- 在Eclipse中制作SSH配置文件提示插件
原文地址:http://blog.csdn.net/longyuhome/article/details/8968093 这篇博客算是对原先的“在Eclipse中制作和使用struts2配置文件提示插 ...
- js之基本包装类型
为了便于操作“基本类型值”,JS 提供了 三个 特殊的引用类型:Boolean.Number.String.这些类型和其他引用类型相似,但同时 也具备 与各自基本类型相应的特殊行为. 实际上:每当读取 ...
- 或许你不知道(2):LinkedList
一,基本的存储结构及数据存取 LinkedList与ArrayList同属List的范畴,ArrayList实现了RandomAccess接口,通过索引随机访问效率较高,而LinkedList提供了直 ...
- svn 检出 Check out 请求的名称有效,但是找不到请求的类型的数据。
根据问题不同有不同的解决方案,可按照以下方法进行解决1.取消TortoiseSVN-网络-代理2.确认SVN目录地址是否正确,可在浏览器中直接打开测试.如地址是由计算机名组成请改成Ip地址进行测试
- 【BZOJ】【3157】&【BZOJ】【3516】国王奇遇记
数论 题解:http://www.cnblogs.com/zhuohan123/p/3726933.html copy一下推导过程: 令$$S_i=\sum_{k=1}^{n}k^im^k$$ 我们有 ...
- 数据类型的处理(提取自FMDB)
if ((!obj) || ((NSNull *)obj == [NSNull null])) { sqlite3_bind_null(pStmt, idx); } // FIXME - someda ...
- js 调用IE内置打印控件
转自学网(http://www.xue5.com/itedu/200802/102909.html) WebBrowser是IE内置的浏览器控件,无需用户下载. 一.WebBrowser控件 < ...
- 对LVS DR模式的理解
Client向vip发请求,lvs接收 Src mac Dst mac type … Src ip Src port Dst ip Dst port … CRC Mac1 Mac2 … … 192.1 ...
- phonegap/cordova常用命令
创建项目 cordova create foldername com.wps.test projectName cd foldername 基本设备信息 设备 API: cordova plugin ...
- flex Chrome flash调试时 出现Shockwave flash has crashed的解决办法
在Chrome中输入:chrome://plugins/ PPAPI的Flash Player停用. 使用NPAPI的Flash player. 这里好像没有显示是Debug版本. 但是我在调 ...