题目:

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的更多相关文章

  1. 293. Flip Game只翻转一步的加减号翻转游戏

    [抄题]: You are playing the following Flip Game with your friend: Given a string that contains only th ...

  2. LeetCode 293. Flip Game

    原题链接在这里:https://leetcode.com/problems/flip-game/description/ 题目: You are playing the following Flip ...

  3. [LeetCode] 293. Flip Game 翻转游戏

    You are playing the following Flip Game with your friend: Given a string that contains only these tw ...

  4. leetcode 293.Flip Game(lintcode 914) 、294.Flip Game II(lintcode 913)

    914. Flip Game https://www.cnblogs.com/grandyang/p/5224896.html 从前到后遍历,遇到连续两个'+',就将两个加号变成'-'组成新的字符串加 ...

  5. [LC] 293. Flip Game

    You are playing the following Flip Game with your friend: Given a string that contains only these tw ...

  6. 【LeetCode】293. Flip Game 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

  7. <String> 186 293 294 249

    186. Reverse Words in a String II 先反转整个字符串,再反转每个单词(调整顺序也可以) 反转单词的时候:当 j 指到最后一个字符的时候,或者 j 的下一个指向空格,则反 ...

  8. [LeetCode] 294. Flip Game II 翻转游戏 II

    You are playing the following Flip Game with your friend: Given a string that contains only these tw ...

  9. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

随机推荐

  1. Android -- 检测耳机插入状态

    原理                                                                                    其实android系统在耳机 ...

  2. “我爱淘”冲刺阶段Scrum站立会议6

    完成任务: 对大部分的布局已经做好了布置. 计划任务: 实现数据库的链接,将页面功能完善. 遇到问题: 使用webservice对数据的提取,用数据库将书的信息展示软件中.

  3. 本地wordpress博客系统安装搭建实践

    我们按步骤来, (1)安装XAMPP集成软件包 wordpress 的运行要求是在 php + MySQL + Apache的服务器环境,所以要先搭建该环境,我用的是XAMPP软件包,安装很方便. 下 ...

  4. Oracle VM VirtualBox 5.0 CentOS 6.4 共享文件夹

    首先在主机(win7)的硬盘建立需要共享文件夹 例如 D:\share_test 然后虚拟机光驱加载Oracle VM VirtualBox安装目录的iso  C:\Program Files\Ora ...

  5. 关于Viual Studio 改变编辑器背景背景及背景图片(转)

    Visual Studio背景颜色或者背景图片是可以修改的,根据个人的爱好进行相应的修改 首先先展示下效果: 修改方法如下: 1.在工具-扩展和更新-联机,此时他会自动搜索,暂时让他自己搜索去吧,这里 ...

  6. Spring MVC 环境搭建(二)

    在Spring MVC 环境搭建(一)中我们知道 spring 的配置是通过 urlmapping 映射到控制器,然后通过实现Controller接口的handlerequest方法转向页面. 但这存 ...

  7. 了解Git

           对于计算机软件初学者来说Git并没有太多了解, 以前没有接触过,但是老师说对其进行了解,也没有什么概念,只有通过上网进行了解 . 了解到的大概内容如下:                 ...

  8. HDU 5294 Tricks Device 最短路+最大流

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5294 题意: 给你个无向图: 1.求最少删除几条边就能破坏节点1到节点n的最短路径, 2.最多能删除 ...

  9. hdu 1533 Going Home 最小费用最大流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1533 On a grid map there are n little men and n house ...

  10. 录制iphone手机视频

    1: 升级自己的手机到ios8, 同时mac os也要升级到10.10 2: 用usb数据线将手机连上电脑. 3: 打开quicktime player创建新movie 4: 选择手机作为音频.视频源 ...