293. Flip Game
2015-12-13 07:58
323 查看
题目:
You are playing the following Flip Game with your friend: Given a string that contains only these two characters:
Write a function to compute all possible states of the string after one valid move.
For example, given
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)。
二刷:
题目的意思是,record all states after one valid move, 所以我们只需要flip一次。 先把String转换为数组,从1开始到最后,把两个连续的'+'变为'-',记录下这个结果,再backtracking把那两个'-'改回去,接着计算下面的结果。遍历完一次数组之后就可以了。
Java:
Time Complexity - O(n), Space Complexity - O(n)。
Reference:
https://leetcode.com/discuss/64248/4-lines-in-java https://leetcode.com/discuss/64335/simple-solution-in-java
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; } }
Reference:
https://leetcode.com/discuss/64248/4-lines-in-java https://leetcode.com/discuss/64335/simple-solution-in-java
相关文章推荐
- 292. Nim Game
- Java菜鸟入门(20) Producer Consumer经典代码
- 揭秘中国四大最顶级圈子,他们手里几乎握着整个民营经济的钱
- jpa使用记录
- 泛型的内部原理:类型擦除以及类型擦除带来的问题
- activity生命周期完整版
- 发送手机物理标识请求
- 框架和类库的区别
- 上传Apk至测试服务器
- 计步器算法简述和模块使用
- 创建动态链接库时设置导出函数的方法
- java框架
- [leetcode] 131. Palindrome Partitioning 解题报告
- 291. Word Pattern II
- Codeforces 602B
- 查看动态链接库中函数参数类型
- 【转】JDK工具jinfo用法详解
- VS2010 + QT5.2+ QT-VS-Addin1.2.2开发环境配置
- [MetaHook] Event Hook
- 蓝桥杯预选反思