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 ...
随机推荐
- [转载]解决zabbix在configure时候遇到的问题(Ubuntu)
http://hi.baidu.com/ryq1xiuluo/item/6d37e658f1b90b13db16351d ./configure --enable-server --enable-ag ...
- 在Action中以Struts2的方式输出JSON数据
参考地址;http://blog.csdn.net/itdada/article/details/21344985
- Careercup - Microsoft面试题 - 5943729928011776
2014-05-10 21:56 题目链接 原题: Suppose you get number of unique users every second from bing For eg, ,,,, ...
- javascript与DOM -- 深入理解javascript
/* 一.文档对象模型Document Object Model DOM(Document Object Model,文档对象模型)是一个通过和JavaScript进行内容交互的API */ /* 注 ...
- 使用Fiddler
右键一个Result,点击Inspect in new Window(Shift+Enter)
- css中的border还可以这样玩
在看这篇文章之前你可能会觉得border只是简单的绘制边框,看了这篇文章,我相信你也会跟我一样说一句"我靠,原来css中的border还可以这样玩".这篇文章主要是很早以前看了别人 ...
- hdu 2853 Assignment KM算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2853 Last year a terrible earthquake attacked Sichuan ...
- 【BZOJ】【1221】【HNOI2001】软件开发
网络流/费用流 说是这题跟餐巾计划一模一样……但我没做过啊……so sad 二分图建模是很好想的,但是要控制流量跟用了的毛巾一样多……oh my god 事实上对于每一天我们无论如何都是要消耗n[i] ...
- Oracle 显示时间问题
在部署的时候. 显示的时间为会 2014/1/1 9:00:00 pm 但开发过程中显示为正常: 2014-1-1 21:00:00 解决方法: 1. Oracle数据库的时间格式没有问题, ...
- 使用feof()函数判断文件是否结束
课本上时这样写的:(用putchar(ch);代表对取出来的字符的处理.) while(!feof(fp)) { ch=fgetc(fp); putchar(ch); } 但是,这样写的话,fgetc ...