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 ...
随机推荐
- 如何维护SSH安全
遇到两次,一次是公司服务器搭建好后,有人尝试ssh暴力破解,auth.log不停出现错误提示 还有买的米国vps,很荣幸地遭到来自波兰的ssh破解尝试 不得不重视ssh的安全 方法: 修改sshd_c ...
- rtsp实时流通过rtmp推送到服务端
很多朋友都会问到rtsp如何通过rtmp协议推送到服务端,正好前段时间开发了这个功能写在这里,和大家分享下. 首先我想说的是:ffmpeg可以实现这个功能.ffmpeg支持rtsp协议,也支持rtmp ...
- 文件读写器FileRW 1.0发布
这个软件未发布前,当年被计算机杂志报道过. FileRW 文件读写器 1.0功能介绍:1.可以以各种方式读普通文件和I/O文件.2.可以以各种方式写文件.3.可以配置文件的分享读写方式.4.可以指定文 ...
- 【BZOJ】【2765】【JLOI2010】铁人双项比赛
计算几何/半平面交 本来我是想去写POJ 1755的,然后想起了这道跟它很像的题,但应该是弱化版,所以就先写了这个…… 我们可以发现每个人的总用时,与k是呈一次函数关系的:$time_i=\frac{ ...
- 在eclipse中配置maven
http://pansanday.blog.163.com/blog/static/381662802012727103454743/ 从eclipse 3.6开始,eclipse有一个marketp ...
- max_flow(Dinic) 分类: ACM TYPE 2014-09-02 15:42 94人阅读 评论(0) 收藏
#include <cstdio> #include <iostream> #include <cstring> #include<queue> #in ...
- fullscreen DXGI DX11
these days i am fullfilling full screen https://github.com/rufelt/simpled3d11window put this one in ...
- vml 在IE8 不显示的问题, Group不能用等问题.
IE8 不显示的问题: <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /> ...
- SSH无密码验证
一.安装和启动SSH协议 sudo yum install ssh sudo yum install rsync service sshd restart 启动服务 (rsync是一个远程数据同步工具 ...
- NGUI 实现 透明底图遮罩 && 人物像素变黑
今天 UI 那边要求实现一个 透明底图遮罩 与 变黑 的效果. 刚开始考虑使用 shader 实现一个 网上搜了一下,发现了这个,但是底图需要不透明才行,不然他会把 底图的不遮罩部分的透明部分 进行颜 ...